In ASP.NET Core 3.x, the usage of IoC containers like Autofac changed. Instead of returning some adapter instance in ConfigureServices
, we have to use the new ConfigureContainer
method.
My question is: how can I access the Autofac IContainer instance in the Configure
method? I tried to call containerBuilder.Build
within ConfigureContainer
, to get a reference to the container instance, but then I get an exception that the container can only built once.
I am well aware that in normal use cases, there should be no need to pass around the container (Service Locator anti pattern etc.....). However, in this special case, we are using a middleware that resolves Command and Event handler types and it is based on Autofac. It needs the container instance.
Any chance to reference the IContainer
instance once it has been built by the framework?
Copyright Notice:Content Author:「mbue」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60303706/asp-net-core-3-x-access-ioc-container