简体   繁体   中英

How to dispose of Autofac container?

I have a static class to setup Autofac registration and its method is called in 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?

In this case you do not need to call it as the AutofacWebApiDependencyResolver is already IDisposable and receives the container as a dependency. It is calling the Dispose when you dispose the AutofacWebApiDependencyResolver

The AutofacWebApiDependencyResolver will be disposed automatically by the system as the HttpConfiguration object inside Configuration already disposes it.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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