ASP.NET Core 3.x - Access IoC Container
NickName:mbue Ask DateTime:2020-02-19T23:25:34

ASP.NET Core 3.x - Access IoC Container

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

Answers
Shahar Shokrani 2020-03-17T21:03:02

In order to get a hold of an IContainer instance you can follow the solution from the autofac documentation (link):\n\n\n// Configure is where you add middleware. This is called after\n// ConfigureContainer. You can use IApplicationBuilder.ApplicationServices \n// here if you need to resolve things from the container. \npublic void Configure(IApplicationBuilder app,\n ILoggerFactory loggerFactory) \n{\n // If, for some reason, you need a reference to the built container, you\n // can use the convenience extension method GetAutofacRoot.\n this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();\n\n loggerFactory.AddConsole(this.Configuration.GetSection(\"Logging\"));\n loggerFactory.AddDebug();\n app.UseMvc(); \n}\n\n\n\nThe GetAutofacRoot(); in an extension method in Autofac.Extensions.DependencyInjection.\n\nSo in order to hold a reference on a casted IContainer you can write:\n\nIContainer container = (IContainer)app.ApplicationServices.GetAutofacRoot();\n\n\nThis cast is valid because IContainer : ILifetimeScope",


More about “ASP.NET Core 3.x - Access IoC Container” related questions

ASP.NET Core 3.x - Access IoC Container

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 questio...

Show Detail

What are the requests in the context of an IoC services container in ASP.NET Core?

What are the requests in the context of an IoC services container in ASP.NET Core? I am learning the ASP.NET Core with the help of these tutorials. And I come across the following excerpt: Scop...

Show Detail

ASP.NET Core with existing IoC container & environment?

I'd like to run the ASP.NET Core web stack along with MVC within a Windows service environment which already hosts an existing application in order to provide a frontend for it. The application uses

Show Detail

Extend Autofac IoC container configuration at runtime in ASP.NET Core

I have an ASP.NET Core 5 MVC application and I use Autofac as my IoC container. To integrate Autofac with ASP.NET Core I use Autofac.Extensions.DependencyInjection package. I'm looking for a way to

Show Detail

Best Practices for IOC Container

I'm using the Unity IOC container and I'm just wondering what is the best best way to access the container for multiple classes. Should every class have an IUnityContainer member and then pass the

Show Detail

Is the Managed Extensibility Framework (MEF) what ASP.NET Core 2 uses internally for IoC?

I am trying to gauge if Managed Extensibility Framework (MEF) is something I can use for extending an ASP.NET Core 2.x MVC website. I saw ExtCore also. My desire is to use it to build infrastruct...

Show Detail

IoC container concept in PHP

I'm trying to grasp the concept of IoC and Dependency Injection in PHP, So iv'e decided to go on and create a small framework and implement these design patterns. i've created my own IoC class whi...

Show Detail

How can I pass an already instantiated IOC container to an Asp.net 5 application?

Using .Net core I have created a console application that self hosts an asp.net 5 for endpoint support (to receive notifications, health checks, etc...). While reading about dependency injection...

Show Detail

How to Replace Built in IoC with my own IoC?

As we all know In asp.net Startup class there is a method ConfigureServices we can add custom Services. Services are made available through Dependency Injection. ASP.NET Core includes a simple b...

Show Detail

What makes an IoC container an IoC container?

So..I've been digging into IoC container and service locator. I thought an IoC container is an IoC container, not a service locator because The way you use it. You pass a service locator to a class

Show Detail