Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 293
Ivan-Mark Debono Avatar asked Oct 20 '22 22:10

Ivan-Mark Debono


1 Answers

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.

like image 112
Ignacio Soler Garcia Avatar answered Oct 22 '22 12:10

Ignacio Soler Garcia