-
Notifications
You must be signed in to change notification settings - Fork 458
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
.NET 6 - Get ServiceProvider Service from MS DI Extension before build() #644
Comments
This is simply not possible. With MS DI you first build a collection of services (IServiceCollection) which will then be compiled into service provider (IServiceProvider). While the service provider has not yet been compiled, none of the services can be resolved. I think the underlying philosophy of MS DI is that DI containers are not configured during runtime. The Windsor Container only receives the service collection when the service provider is built (during IHostBuilder.Build). |
You are correct that in Microsoft.Extensions.DependencyInjection (MS DI), dependency injection is primarily configured during the startup phase of the application, and not at runtime. It follows a two-step process:
Once the IServiceProvider is built, it is generally not recommended to modify it during runtime, as this could lead to unexpected behavior and threading issues. However, there might be some scenarios where you need to update the DI container at runtime. In such cases, you can utilize a factory pattern or other workarounds to achieve this. For example, you can register a factory in the DI container that is responsible for creating and managing instances of a particular service. The factory can then be updated during runtime to change the way the service is created or to use a different implementation. This approach still adheres to the MS DI philosophy but provides some flexibility in managing service instances. Remember, though, that runtime modifications should be used with caution and only when necessary, as they might lead to complications or unintended side effects. |
FYI - I am using MS DI and Castle Windsor container together; I simply register IServiceProvide with Windsor. |
Maybe something like this may work: `using System; internal class Program
} |
Please consider the above problematic statement, how could I resolve the imtermediate registered service in
IWindsorInstaller
? Since theHostBuilder
has not yet built, I cannot resolve component. This may lead to the resolving problem for MS DI orginal registered components likeIConfiguration
Any advise is much appreciated. Thanks
The text was updated successfully, but these errors were encountered: