简体   繁体   English

ASP.NET Core 3.x - 访问 IoC 容器

[英]ASP.NET Core 3.x - Access IoC Container

In ASP.NET Core 3.x, the usage of IoC containers like Autofac changed.在 ASP.NET Core 3.x 中,Autofac 等 IoC 容器的使用发生了变化。 Instead of returning some adapter instance in ConfigureServices , we have to use the new ConfigureContainer method.我们必须使用新的ConfigureContainer方法,而不是在ConfigureServices中返回一些适配器实例。

My question is: how can I access the Autofac IContainer instance in the Configure method?我的问题是:如何在Configure方法中访问 Autofac IContainer 实例? 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.我尝试在ConfigureContainer调用containerBuilder.Build以获取对容器实例的引用,但随后我得到一个异常,即容器只能构建一次。

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.但是,在这种特殊情况下,我们使用了一个中间件来解析命令和事件处理程序类型,它基于 Autofac。 It needs the container instance.它需要容器实例。

Any chance to reference the IContainer instance once it has been built by the framework?一旦框架构建了IContainer实例,有机会引用它吗?

If you really need to have dependency on the container you should add a dependency on ILifetimeScope and then resolve from this scope.如果你真的需要依赖容器,你应该添加对ILifetimeScope的依赖,然后从这个范围解析。

public class FooMiddleware
{
    public FooMiddleware(RequestDelegate next)
    {
        this._next = next;
    }

    private readonly RequestDelegate _next;


    public async Task InvokeAsync(HttpContext context, ILifetimeScope scope)
    {
        scope.Resolve<Foo>(); 
        await this._next(context);
    }
}

In order to get a hold of an IContainer instance you can follow the solution from the autofac documentation ( link ):为了获得IContainer实例,您可以按照 autofac 文档( 链接)中的解决方案进行操作:

 // Configure is where you add middleware. This is called after // ConfigureContainer. You can use IApplicationBuilder.ApplicationServices // here if you need to resolve things from the container. public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { // If, for some reason, you need a reference to the built container, you // can use the convenience extension method GetAutofacRoot. this.AutofacContainer = app.ApplicationServices.GetAutofacRoot(); loggerFactory.AddConsole(this.Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseMvc(); }

The GetAutofacRoot(); GetAutofacRoot(); in an extension method in Autofac.Extensions.DependencyInjection .Autofac.Extensions.DependencyInjection的扩展方法中。

So in order to hold a reference on a casted IContainer you can write:因此,为了在铸造的IContainer上保持引用,您可以编写:

IContainer container = (IContainer)app.ApplicationServices.GetAutofacRoot();

This cast is valid because IContainer : ILifetimeScope这个转换是有效的,因为IContainer : ILifetimeScope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core与现有的IoC容器和环境? - ASP.NET Core with existing IoC container & environment? .NET Standard和.NET Core 3.x或ASP.NET Core 3.x - .NET Standard and .NET Core 3.x or ASP.NET Core 3.x 在 Rider 中反编译 ASP.NET Core 3.x 源代码 - Decompile ASP.NET Core 3.x sources in Rider 在 asp.net core 3.x 中按需运行后台任务 - Running background task on demand in asp.net core 3.x 将所有注册的服务从 ASP.NET Core IOC 容器解析到单个接口 - Resolve all registered services to a single interface from ASP.NET Core IOC container Autofac 作为 AWS 中的 IoC 容器 Lambda 无服务器 ASP.NET Core 3.1 Web ZDB974238714CA8DE64FZACE7 - Autofac as IoC container in AWS Lambda Serverless ASP.NET Core 3.1 Web API 使用 ASP.NET Core 内置 IoC 容器注入多个客户端而不是一个 - Injecting multiple clients instead of just one using ASP.NET Core built-in IoC container 在 ASP.NET Core 3.x 中使用 [FromRoute] 绑定到复杂的 object 给出 null - Bind to complex object using [FromRoute] in ASP.NET Core 3.x gives null 如何为 ASP.NET Core 3.x 项目设置 MyControllerFactory - How to set up MyControllerFactory for ASP.NET Core 3.x project 如何在 3.x 中取回 ASP.NET 核心详细请求登录 - How can I get back ASP.NET Core verbose request logging in 3.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM