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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With