title: HttpContext.Current is null inside Identity Framework's methods
body: <p>I am using ASP.NET MVC 5 and Identity Framework. When I call  UserManager.UpdateAsync(...) my eventhandlers on ApplicationDbContext() SaveChanges will run. Here I am using HttpContext.Current for different purposes (logging and auditing) so I must get say current user. However the whole method runs in a worker thread, and here HttpContext.Current is null. </p>
  
  <p>The biggest problem that the UserManager's "sync" methods are only wrappers around the async version, so the calls are serialized, but the methods (and eventhandlers) still run in a different worker thread. </p>
  
  <p>Please note this issue has nothing to do with the async/await context. In the controller after the await (or calling the 'sync' version) I have back the correct HttpContext, even the controller's method is continuing in an other thread. That's fine.</p>
  
  <p>So the problem is inside the async worker which will run in both the "sync" and async versions. I think I am understanding the phenomena (but I am not happy with the fake 'sync' method versions, real sync methods would not exhibit this issue.) I just does not know how to deal/workaround it.</p>
  
  <p>[btw: Would not it be more natural to implement UserManager's operarations as simple pure sync versions, then wrap them by async multithreaded wrappers?. IF we continue this async fashion without thinking we will soon invent the async assignment operator. It costs me dozens of hours (just this issue), and costs worldwide zillion dollars, I am sure in many cases less return than its price.] </p>
  
  <p>Bonus: We are talking about UserManager which's impact pretty marginal, but the same principles and issues can apply any out of the box library (black box for you) which authors do not implement sync versions and or do not care about the controller thread's context. What about EF, it is not so marginal... and what about DI containers instantiation infrastructure like "request scope" or "session scope". Surely they misbehave if resolving occurs in a thread with no HttpContext.Current. Recently I refreshed SendGrid NuGet, and (as a breaking change) Deliver() method gone, and now only DeliverAsync() is existing...</p>
  
  <p>I would like to have a safe reliable way, how can I access the HttpContext inside this worker for logging and audit purposes.</p>
  
  <p>Sample code, the controller 'sync' version:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
  public virtual ActionResult Edit(ApplicationUser user)
  {
      // validation etc
      // Update() seems to be only a poor wrapper around the async version, still uses a worker thread.
      var result = UserManager.Update(user);
      // Note: HttpContext is correct here so it is not an async/await problem
  
      // error handling, creating ActionResult etc.
  }
  </code></pre>
  
  <p>Sample code, the controller async version:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
  public virtual async Task&lt;ActionResult&gt; Edit(ApplicationUser user)
  {
      // validation etc
      var result = await UserManager.UpdateAsync(user);
      // Note: HttpContext is correct here so it is not an async/await problem
  
      // error handling, creating ActionResult etc.
  }
  </code></pre>
  
  <p>and the event handler where HttpContext is null:</p>
  
  <pre><code>public ApplicationDbContext() : base("DefaultConnection", false)
  {
      InitializeAudit();
  }
  
  private void InitializeAudit()
  {
      var octx = ((IObjectContextAdapter) this).ObjectContext;
  
      octx.SavingChanges +=
          (sender, args) =&gt;
          {
              // HttpContext.Current is null here
          };
  }
  </code></pre>
  
  <p>Any ideas?</p>