title: Lifetime of dbContext when using Async Controllers via dependency injection
body: <p>In the past I have used AutoFac to inject a EntityFramework DB context to various services on a <strong>InstancePerRequest</strong> schedule.</p>
  
  <pre><code>builder.RegisterType&lt;MyDataContext&gt;()
         .As&lt;IDataContext&gt;()
         .As&lt;IUnitOfWork&gt;()
         .InstancePerRequest(); 
  </code></pre>
  
  <p>This has allowed me to share the context across services when injecting multiple services into a controller.</p>
  
  <pre><code>// Note each of these services take a IDataContext via constructor injection
  public FilesController(
              IAnalysisService analysisService,
              IUserService userService)
  {
  
  }
  </code></pre>
  
  <p>I have an action filter that commits my Context at the end of each action request (I am probably looking at changing this but for now it suites my purpose of this question).</p>
  
  <pre><code>public class UnitOfWorkActionAttribute : ActionFilterAttribute
  {
      public IUnitOfWork UnitOfWork { get; set; }
  
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
          UnitOfWork.SaveChangesAsync().WaitAndUnwrapException();
      }
  }
  </code></pre>
  
  <p>My reading of async in MVC is that the thread that started the request is not guaranteed to finish it due to it being freed up when using <code>await</code>.</p>
  
  <p>In AutoFac injection does this mean that the <strong>UnitOfWork</strong> instance injected into the <strong>ActionFilterAttribute</strong> might differ from the one injected into the Controller or is <strong>InstancePerRequest</strong> not effected by changes to the Thread handling the request?</p>