-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dependency injection with convenience methods #6783
Comments
@guardrex that doesn't really answer the question, it bypasses DI altogether. There's no direct way to get services into ConfigureServices. There are some indirect ways where you register your thing that needs a service as its own service and then resolve it later outside of ConfigureServices. People sometimes do this with IConfigureOptions. In Configure you can call |
Yes. I understand. @breyed You're looking for more than a hack to get a console logger?
Sounds good. I'll pitch a PR for that ... when I can. 🏃😅 |
Interesting point: IWebHostBuilder.ConfigureServices is not equivalent to Startup.ConfigureServices. IWebHostBuilder.ConfigureServices is called first and adds services to the host's DI container. That's why no DI services are available at this stage. Startup.ctor can consume the host's services and then Startup.ConfigureServices builds a secondary container. |
It's a chicken and egg problem. You need a container to activate the Startup class so we have a bootstrapping container for this reason. When you're not using Startup there's nothing to activate and thus no way to get services. We don't have a ConfigureApplicationServices on WebHostBuilder which would be the equivalent of what Startup offers today. |
Which means that we can expand our example for Otherwise for the convenience methods, I'll just pitch that example with |
FWIW, the reason this question even arose for me is not because I wanted to log the startup process. Rather, in ConfigureServices, in my This was all well and good (at least it worked - I can't say whether it was the best approach) until ASP.NET Core 2.1, when the Based on the discussion here and in aspnet/Hosting#1150, it seems like ConfigureServices comes too early to access the logging service that will be used when model binding occurs. Not knowing how this is supposed be get hooked up, I decided to punt with a |
Same problem here. I want to add
There is no longer a ResponseCacheFilter constructor that doesn't require an ILoggerFactory. |
Using the convenience methods, how can I access services such as ILoggerFactory provided by the hosting service? Using Startup, they are available via dependency injection in the constructor and Configure. What is the equivalent for the convenience methods?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: