title: Null reference exception using Microsoft.Unity on DisposeOfChildContainer()
body: <p>I am working on mvc-5 application which also has webapi. Application is throwing System.NullReference Exception.</p>
  
  <p>Error message</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  
  Stack trace
  
  at Unity.Mvc5.UnityDependencyResolver.DisposeOfChildContainer()
  
  at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>This is how the container is built and is called from Application_Start in global as UnityBootstrapper.Initialize(); -</p>
  
  <pre><code>public static class UnityBootstrapper
  {
      public static void Initialize()
      {
          var container = BuildUnityContainer();
          DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
          GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
          ControllerBuilder.Current.SetControllerFactory(new UnityMvcControllerFactory(container));
      }
      private static IUnityContainer BuildUnityContainer()
      {
          var container = new UnityContainer();
  
          // MVC/WebApi controller factories
          container.RegisterInstance&lt;IHttpControllerActivator&gt;(new UnityApiControllerActivator(container,
              new DefaultHttpControllerActivator()));
  
          // NHibernate sessions            
          container.RegisterInstance&lt;ISessionBuilder&gt;(SessionBuilder.Instance);
          container.RegisterType&lt;IClientCodeRequestContext, ClientCodeRequestContext&gt;(
              new CurrentRequestContextLifetimeManager&lt;IClientCodeRequestContext&gt;());
          container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;(
              new CurrentRequestContextLifetimeManager&lt;IUnitOfWork&gt;(),
              new InjectionConstructor(container.Resolve&lt;ISessionBuilder&gt;()));
          container.RegisterType&lt;IUnitOfWorkContext&gt;(
              new InjectionFactory(c =&gt; container.Resolve&lt;IUnitOfWork&gt;().CommonContext));
          container.RegisterType&lt;IUnitOfWorkClientContext&gt;(
              new InjectionFactory(
                  c =&gt; container.Resolve&lt;IUnitOfWork&gt;()[container.Resolve&lt;IClientCodeRequestContext&gt;().ClientCode]));
  
          //Repositories
          container.RegisterType&lt;IUserRepository, UserRepository&gt;();
          //Business Logic
          container.RegisterType&lt;IMembershipPrincipalBusiness, MembershipPrincipalBusiness&gt;();
          // Other
          container.RegisterType&lt;IAuthenticationProvider, CoreMembershipProvider&gt;();
          return container;
      }
  }
  </code></pre>
  
  <p>In global.asax.cs-</p>
  
  <pre><code>protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
  
      {
          var principalBusiness = DependencyResolver.Current.GetService&lt;IMembershipPrincipalBusiness&gt;();
      }
  </code></pre>
  
  <p>The issue is in production only and is caught by newrelic.</p>
  
  <p>Is the exception a bug in unity or something is missed in this implementation. I have been searching the issue since a week but could not get anything.</p>