title: Resolving IOwinContext in MVC5 application using Autofac
body: <p>I have trouble using <code>MembershipReboot</code> with the new ASP MVC5 template and <code>Autofac</code>. I have used the default MVC5 template to set up the site and then tried to wire up the <code>MembershipReboot</code> framework as a replacement for the ASP Identity framework that ships with the template.</p>
  
  <p>This issue I am having is trying to resolve an <code>IOwinContext</code> from the <code>Autofac</code> container. Here is my wiring in the Startup class (cut down to basics). This is the wiring used in the samples for the <code>MembershipReboot Owin</code> application (except there he uses Nancy).</p>
  
  <pre><code>public partial class Startup
   {
      public void Configuration(IAppBuilder app)
      {
          var builder = new ContainerBuilder();
          builder.RegisterControllers(Assembly.GetExecutingAssembly());
  
          builder.Register(c =&gt; new DefaultUserAccountRepository())
              .As&lt;IUserAccountRepository&gt;()
              .As&lt;IUserAccountQuery&gt;()
              .InstancePerLifetimeScope();
  
          builder.RegisterType&lt;UserAccountService&gt;()
              .AsSelf()
              .InstancePerLifetimeScope();
  
          builder.Register(ctx =&gt;
          {
              **var owin = ctx.Resolve&lt;IOwinContext&gt;();** //fails here
              return new OwinAuthenticationService(
                  MembershipRebootOwinConstants.AuthenticationType,
                  ctx.Resolve&lt;UserAccountService&gt;(),
                  owin.Environment);
          })
              .As&lt;AuthenticationService&gt;()
              .InstancePerLifetimeScope();
  
          var container = builder.Build();
          DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  
          ConfigureAuth(app);
          app.Use(async (ctx, next) =&gt;
          {
              using (var scope = container.BeginLifetimeScope(b =&gt;
              {
                  b.RegisterInstance(ctx).As&lt;IOwinContext&gt;();
              }))
              {
                  ctx.Environment.SetUserAccountService(() =&gt; scope.Resolve&lt;UserAccountService&gt;());
                  ctx.Environment.SetAuthenticationService(() =&gt; scope.Resolve&lt;AuthenticationService&gt;());
                  await next();
              }
          });
      }
  </code></pre>
  
  <p>And here is my controller with the dependency specified in the controller constructor.</p>
  
  <pre><code>public class HomeController : Controller
  {
      private readonly AuthenticationService service;
  
      public HomeController(AuthenticationService service)
      {
          this.service = service;
      }
  
      public ActionResult Index()
      {
          return View();
      }
  
      public ActionResult About()
      {
          ViewBag.Message = "Your application description page.";
  
          return View();
      }
  
      public ActionResult Contact()
      {
          ViewBag.Message = "Your contact page.";
  
          return View();
      }
  }
  </code></pre>
  
  <p>It seems I need to wrap my <code>Autofac</code> container in an <code>AutofacDependencyResolver</code> in order for the MVC framework to use the container to resolve components. This is the only major difference from the <code>Nancy Owin</code> sample and my use in MVC5.</p>
  
  <p>When I do this, it then seems (from my Tracing) as if the dependency is being resolved without first going through the <code>OWIN middleware</code> stack so the <code>IOwinContext</code> is never registered.</p>
  
  <p>What am I doing wrong here?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Brock, your new sample works perfectly when I migrate the configuration over to my project. Just for my understanding, it seems that this line in your new sample registers the current OwinContext with the container and that is what was missing previously.</p>
  
  <pre><code>builder.Register(ctx=&gt;HttpContext.Current.GetOwinContext()).As&lt;IOwinContext&gt;();
  </code></pre>
  
  <p>Is that</p>