Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:39:25+00:00 2026-06-08T20:39:25+00:00

I am developing an ASP.Net MVC 3 Web application which uses Unity 2.0 as

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:39:27+00:00Added an answer on June 8, 2026 at 8:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are currently developing a web application in ASP.NET MVC which would really benefit
I am developing an application which is web-based (asp.net MVC). The user configures reminders
I am developing an ASP.Net MVC 3 Web application using Razor Views. I have
I am developing an ASP.Net MVC 3 Web application with Entity Framework 4.1 and
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1 and
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1, however,
I am developing an ASP.Net MVC 3 Web application and I am having some
I am developing my first ASP.Net MVC 3 web application against a SQL Server
I'm developing a web application using ASP .NET MVC 1.0 and jQuery (including the
I'm developing a ASP.NET MVC 2 web application. So far, I managed to define

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.