简体   繁体   English

如何处理Autofac容器?

[英]How to dispose of Autofac container?

I have a static class to setup Autofac registration and its method is called in Application_Start. 我有一个静态类来设置Autofac注册,它的方法在Application_Start中调用。 Something like this: 像这样的东西:

public static class RegisterAutofac
{
    public static void Setup()
    {
        var config = GlobalConfiguration.Configuration;
        var builder = new ContainerBuilder();

        //Do registration here...

        var container = builder.Build();
        var resolver = new AutofacWebApiDependencyResolver(container);

        GlobalConfiguration.Configuration.DependencyResolver = resolver;
    }
}

So far I haven't found examples that dispose the container. 到目前为止,我还没有找到处理容器的示例。

Is this enough or should I return the container and then dispose of the container in Dispose method in Global.asax? 这是否足够或者我应该返回容器然后在Global.asax中的Dispose方法中处理容器?

In this case you do not need to call it as the AutofacWebApiDependencyResolver is already IDisposable and receives the container as a dependency. 在这种情况下,您不需要调用它,因为AutofacWebApiDependencyResolver已经是IDisposable并且将容器作为依赖项接收。 It is calling the Dispose when you dispose the AutofacWebApiDependencyResolver 当你处理AutofacWebApiDependencyResolver时它正在调用Dispose

The AutofacWebApiDependencyResolver will be disposed automatically by the system as the HttpConfiguration object inside Configuration already disposes it. AutofacWebApiDependencyResolver将由系统自动处理,因为Configuration中的HttpConfiguration对象已经处理它。

Hope it helps. 希望能帮助到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM