title: Issue with Autofac 2 and MVC2 using HttpRequestScoped
body: <p>I'm running into an issue with Autofac2 and MVC2.  The problem is that I am trying to resolve a series of dependencies where the root dependency is HttpRequestScoped.  When I try to resolve my UnitOfWork (which is Disposable), Autofac fails because the internal disposer is trying to add the UnitOfWork object to an internal disposal list which is null.  Maybe I'm registering my dependencies with the wrong lifetimes, but I've tried many different combinations with no luck.  The only requirement I have is that MyDataContext lasts for the entire HttpRequest.</p>
  
  <p>I've posted a demo version of the code for <a href="http://www.mediafire.com/?dwlyzzjkygg" rel="nofollow noreferrer">download here</a>.</p>
  
  <p>Autofac modules are set up in web.config</p>
  
  <p><strong>Global.asax.cs</strong></p>
  
  <pre><code>protected void Application_Start()
  {
      string connectionString = "something";
  
      var builder = new ContainerBuilder();
  
      builder.Register(c =&gt; new MyDataContext(connectionString)).As&lt;IDatabase&gt;().HttpRequestScoped();
      builder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;().InstancePerDependency();
      builder.RegisterType&lt;MyService&gt;().As&lt;IMyService&gt;().InstancePerDependency();
  
      builder.RegisterControllers(Assembly.GetExecutingAssembly());
  
      _containerProvider = new ContainerProvider(builder.Build());
      IoCHelper.InitializeWith(new AutofacDependencyResolver(_containerProvider.RequestLifetime));
  
      ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));
  
      AreaRegistration.RegisterAllAreas();
      RegisterRoutes(RouteTable.Routes);
  }
  </code></pre>
  
  <p><strong>AutofacDependencyResolver.cs</strong></p>
  
  <pre><code>public class AutofacDependencyResolver
  {
      private readonly ILifetimeScope _scope;
  
      public AutofacDependencyResolver(ILifetimeScope scope)
      {
          _scope = scope;
      }
  
      public T Resolve&lt;T&gt;()
      {
          return _scope.Resolve&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p><strong>IoCHelper.cs</strong></p>
  
  <pre><code>public static class IoCHelper
  {
      private static AutofacDependencyResolver _resolver;
  
      public static void InitializeWith(AutofacDependencyResolver resolver)
      {
          _resolver = resolver;
      }
  
      public static T Resolve&lt;T&gt;()
      {
          return _resolver.Resolve&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p><strong>UnitOfWork.cs</strong></p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      void Commit();
  }
  
  public class UnitOfWork : IUnitOfWork
  {
      private readonly IDatabase _database;
  
      public UnitOfWork(IDatabase database)
      {
          _database = database;
      }
  
      public static IUnitOfWork Begin()
      {
          return IoCHelper.Resolve&lt;IUnitOfWork&gt;();
      }
  
      public void Commit()
      {
          System.Diagnostics.Debug.WriteLine("Commiting");
          _database.SubmitChanges();
      }
  
      public void Dispose()
      {
          System.Diagnostics.Debug.WriteLine("Disposing");
      } 
  }
  </code></pre>
  
  <p><strong>MyDataContext.cs</strong></p>
  
  <pre><code>public interface IDatabase
  {
      void SubmitChanges();
  }
  
  public class MyDataContext : IDatabase
  {
      private readonly string _connectionString;
  
      public MyDataContext(string connectionString)
      {
          _connectionString = connectionString;
      }
  
      public void SubmitChanges()
      {
          System.Diagnostics.Debug.WriteLine("Submiting Changes");
      }
  }
  </code></pre>
  
  <p><strong>MyService.cs</strong></p>
  
  <pre><code>public interface IMyService
  {
      void Add();
  }
  
  public class MyService : IMyService
  {
      private readonly IDatabase _database;
  
      public MyService(IDatabase database)
      {
          _database = database;
      }
  
     public void Add()
     {
         // Use _database.
     }
  }
  </code></pre>
  
  <p><strong>HomeController.cs</strong></p>
  
  <pre><code>public class HomeController : Controller
  {
      private readonly IMyService _myService;
  
      public HomeController(IMyService myService)
      {
          _myService = myService;
      }
  
      public ActionResult Index()
      {
          // NullReferenceException is thrown when trying to
          // resolve UnitOfWork here.
          // Doesn't always happen on the first attempt.
          using(var unitOfWork = UnitOfWork.Begin())
          {
              _myService.Add();
  
              unitOfWork.Commit();
          }
  
          return View();
      }
  
      public ActionResult About()
      {
          return View();
      }
  }
  </code></pre>