title: Unity PerRequestLifetimeManager re-using object in different requests
body: <p>I've set up Unity for dependency injection for our project. The project itself is an ASP.NET application that uses both MVC and Web API.</p>
  
  <p>For the database context, I'm using the <code>PerRequestLifetimeManager</code>. This is done so that the different bits of business logic are using the same context (and thus the same transaction).</p>
  
  <p>In order to be able to use the <code>PerRequestLifetimeManager</code>, I've added references to the nuget packages <em>Unity bootstrapper for ASP.NET MVC</em> and <em>Unity bootstrapper for ASP.NET Web API</em>.</p>
  
  <p>For use of this lifetime manager in Web API, the following line has been added to the startup code:</p>
  
  <pre><code>Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
  </code></pre>
  
  <p>The Unity container is set up for both MVC and Web API:</p>
  
  <pre><code>var container = BuildUnityContainer();
  GlobalConfiguration.Configuration.DependencyResolver = new Microsoft.Practices.Unity.WebApi.UnityDependencyResolver(container);
  System.Web.Mvc.DependencyResolver.SetResolver(new Microsoft.Practices.Unity.Mvc.UnityDependencyResolver(container));
  </code></pre>
  
  <p>In building the Unity container, the database context is set up to be resolved per request in the following way:</p>
  
  <pre><code>container.RegisterType&lt;IDataContext&gt;(new PerRequestLifetimeManager(),
      new InjectionFactory(c =&gt;
      {
          // Some code
          return new DataContext(/* params */);
      }
  ));
  </code></pre>
  
  <p>However, it seems that this code is not giving me a new <code>DataContext</code> for each request. It is giving me the same context in different places within a single request (which is fine). However, subsequent (web api) requests are being given the same instance of <code>DataContext</code> where I would expect a new one to be created for each new request. I also would expect the <code>DataContext</code> to be properly disposed of after the request is finished (the class implements <code>IDisposable</code>).</p>
  
  <p>What's going on here? Am I missing a bit of configuration to make this work properly? Or isn't this supposed to work the way I expect it to?</p>