简体   繁体   中英

ASP.Net MVC 3 Unity IoC Disposal

I am developing an ASP.Net MVC 3 Web application which uses Unity 2.0 as it's IoC container.

Below shows an example of my Application_Start() method in my Global.asax file

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    IUnityContainer container = new UnityContainer();

    container.RegisterType<IControllerActivator, CustomControllerActivator>(
        new HttpContextLifetimeManager<IControllerActivator>());

    //container.RegisterType<IUnitOfWork, UnitOfWork>(
    //  new ContainerControlledLifetimeManager());
    container.RegisterType<IUnitOfWork, UnitOfWork>(
        new HttpContextLifetimeManager<IUnitOfWork>());

    container.RegisterType<IListService, ListService>(
        new HttpContextLifetimeManager<IListService>());
    container.RegisterType<IShiftService, ShiftService>(
        new HttpContextLifetimeManager<IShiftService>());

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

My HttpContextLifetimeManager looks like this

public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
{
    public override object GetValue()
    {
        return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
    }

    public override void RemoveValue()
    {
        HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
    }

    public override void SetValue(object newValue)
    {
        HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] =
            newValue;
    }

    public void Dispose()
    {
        RemoveValue();
    }
}

My issue is that, the method Dispose() in the above class is never called when I put a breakpoint on it. I am worried that my IoC container instance is never being disposed of. Could this lead to problems?

I found this snippet of code which I placed in my Global.asax file, but still the Dispose() method never gets called

protected void Application_EndRequest(object sender, EventArgs e)
{
    using (DependencyResolver.Current as IDisposable);
}

Can anyone help me with how to dispose of each instance of my Unity container?

Thanks.

Use the Unity.MVC3 nuget package. Then when initializing specify the HierarchicalLifetimeManager and your objects will be disposed of after each request.

container.RegisterType(new HierarchicalLifetimeManager());

It's as simple as that : )

Unity does not track the instances it creates nor does it dispose of them. Rory Primrose has an extension that does the tracking and allows for the disposal of objects by calling container.TearDown() .

LifetimeManagers that clean up after themselves are on the wishlist for Unity vNext .

Bootstrapping a new container instance is expensive if you do it on every request. So I would consider caching the container instance after you are done with all the registrations.

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