title: async await with .net 4.0 - maintain static context
body: <p>I have an MVC 4.0 site which is using the BCL async/await package.
  To preserve HttpContext across the initial thread and the subsequent thread (after the await) I began by creating a closure with a reference to the context like so:</p>
  
  <pre><code>   public async Task&lt;ViewResult&gt; GetCustomer(int id)
        {            
         var ctx =  HttpContext.Current;
         ctx["test"] = "test";
  
         await DoSomeLongRunningIO();
  
         var test = ctx["test"];
         //do other things with context
  
         return View();
      }
  </code></pre>
  
  <p>However, I need to call various services after the await. These legacy services call the HttpContext.Current directly. 
  So I approached this with the following code, which seemed like a simple way to ensure these services still work as expected.</p>
  
  <pre><code>  public async Task&lt;ViewResult&gt; GetCustomer(int id)
        {            
         var ctx =  HttpContext.Current;
  
         await DoSomeLongRunningIO();
  
         HttpContext.Current = ctx;
  
         //call other services which use static references to httpcontext
         return View();
      }
  </code></pre>
  
  <p>This does work, however I have been reading <a href="https://stackoverflow.com/questions/13557798/preserve-httpcontext-when-going-async-with-webapi-medium-trust">this answer that suggests it is a bad idea:</a></p>
  
  <ol>
  <li>Is this a bad idea, and if so why is it worse than the first example?</li>
  </ol>
  
  <p>Thanks for your time.</p>