title: Autofac OWIN TestServer and HttpContext
body: <p>I am trying to setup Integration tests with my IIS Hosted WebAPI 2.2 application. I use Autofac for DI and I am using the new ASP.net Identity stack which uses OWIN. I am running into an issue with Autofac where the HttpContext class is always null. Here is how I am setting up my base integration test class-</p>
  
  <pre><code> [TestClass]
  public class TestBase
  {
      private SimpleLifetimeScopeProvider _scopeProvider;
      private IDependencyResolver _originalResolver;
      private HttpConfiguration _configuration;
      public TestServer Server { get; private set; }
  
      [TestInitialize]
      public void Setup()
      {
          Server = TestServer.Create(app =&gt;
          {
              //config webpai
              _configuration = new HttpConfiguration();
              WebApiConfig.Register(_configuration);
  
              // Build the container.
              var container = App_Start.IocConfig.RegisterDependencies(_configuration);
              _scopeProvider = new SimpleLifetimeScopeProvider(container);
  
              //set the mvc dep resolver
              var mvcResolver = new AutofacDependencyResolver(container, _scopeProvider);
              _originalResolver = DependencyResolver.Current;
              DependencyResolver.SetResolver(mvcResolver);
  
              //set the webapi dep resolvers
              _configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
  
              app.UseAutofacMiddleware(container);
              app.UseAutofacWebApi(_configuration);
              app.UseAutofacMvc();
          });
      }
  
      [TestCleanup]
      public void Cleanup()
      {
          // Clean up the fake 'request' scope.
          _configuration.Dispose();
          DependencyResolver.SetResolver(_originalResolver);
          _scopeProvider.EndLifetimeScope();
          Server.Dispose();
      }
  }
  </code></pre>
  
  <p>When a simple test starts, I get an ArgumentNullException "Value cannot be null" httpContext. Which if I track down into the autofac code, I think it is coming from this extension method -</p>
  
  <pre><code> public static class AutofacMvcAppBuilderExtensions
      {
          internal static Func&lt;HttpContextBase&gt; CurrentHttpContext = () =&gt; new HttpContextWrapper(HttpContext.Current);
  
          /// &lt;summary&gt;
          /// Extends the Autofac lifetime scope added from the OWIN pipeline through to the MVC request lifetime scope.
          /// &lt;/summary&gt;
          /// &lt;param name="app"&gt;The application builder.&lt;/param&gt;
          /// &lt;returns&gt;The application builder.&lt;/returns&gt;
          [SecuritySafeCritical]
          [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
          public static IAppBuilder UseAutofacMvc(this IAppBuilder app)
          {
              return app.Use(async (context, next) =&gt;
              {
                  var lifetimeScope = context.GetAutofacLifetimeScope();
                  var httpContext = CurrentHttpContext();
  
                  if (lifetimeScope != null &amp;&amp; httpContext != null)
                      httpContext.Items[typeof(ILifetimeScope)] = lifetimeScope;
  
                  await next();
              });
          }
      }
  </code></pre>
  
  <p>loacted in the <a href="https://github.com/autofac/Autofac/blob/master/Core/Source/Autofac.Integration.Mvc.Owin/AutofacMvcAppBuilderExtensions.cs" rel="nofollow">Core/Source/Autofac.Integration.Mvc.Owin/AutofacMvcAppBuilderExtensions.cs</a> file. Is there a problem with my setup, or a proper way to use Autofac in integration tests with a WebApi application using IIS Host and OWIN Middleware? </p>