title: Web API Service - How to use "HttpContext.Current" inside async task
body: <p>I'm using a "Post" async method of webApi rest service:</p>
  
  <pre><code>public async Task&lt;object&gt; Post([FromBody]string data)
  {
        object response = ExecuteServerLogics(data);
  
        return response;
  }
  </code></pre>
  
  <p>This above code worked good but in some of the client's calls, we experienced performance issues.</p>
  
  <p>After reading some articles here, i've noticed that our webApi rest service,
  is not really working asynchronously with its incoming web requests,
  because we forgot to use <strong>async/await pattern</strong> :</p>
  
  <pre><code>public async Task&lt;object&gt; Post([FromBody]string data)
  {
        object response = await Task&lt;object&gt;.Run( () =&gt;
        {
             return ExecuteServerLogics(data);
        });
  
        return response;
  }
  </code></pre>
  
  <p>After this fix we noticed the performance got better,
  but we found another critic problem: 
  when accessing <strong>HttpContext.Current</strong> - it returns Null reference:</p>
  
  <pre><code>public async Task&lt;object&gt; Post([FromBody]string data)
  {
        object response = await Task&lt;object&gt;.Run( () =&gt;
        {
             var currentContext = HttpContext.Current; // Returns Null!
             return ExecuteServerLogics(data);
        });
  
        return response;
  }
  </code></pre>
  
  <p>We tried to found a solution for it, and in most posts we found that we should pass the
  worker thread's <strong>HttpContext</strong> reference into the inner Task that executes the server logics.
  The problem with this solution is that the server's logics methods, use many static classes that use
  <strong>"HttpContext.Current"</strong> such as - </p>
  
  <ol>
  <li>Loggers calls.</li>
  <li>static security classes that retrieves the user.identity</li>
  <li>static security classes that retrives the incoming request's session data, etc.</li>
  </ol>
  
  <p>Therefore, passing the <strong>"HttpContext.Current"</strong> reference of the worker thread won't solve it.</p>
  
  <p>When we tried the next solution: </p>
  
  <pre><code>public async Task&lt;object&gt; Post([FromBody]string data)
      {
            // Save worker context:
            var currentContext = HttpContext.Current; 
  
            object response = await Task&lt;object&gt;.Run( () =&gt;
            {
                 // Set the context of the current task :
                 HttpContext.Current = currentContext ; // Causes the calls not to work asynchronously for some reason!
  
                 // Executes logics for current request:
                 return ExecuteServerLogics(data);
            });
  
            return response;
      }
  </code></pre>
  
  <p>for some reason, we noticed the performance got worse again, like it had returned working synchronously again.</p>
  
  <p><strong><em>Our problems are:</em></strong></p>
  
  <p><strong>1.</strong> Why in the last example, setting the <strong>"HttpContext.Current"</strong> inside the await task,
  causes the requests to return the same bad performance results which similar to the <strong>synchronous</strong> results?</p>
  
  <p><strong>2.</strong> Is there another way we can use <strong>"HttpContext.Current"</strong> inside the inner task that call - "ExecuteServerLogics",
  and in all the static classes which also call <strong>"HttpContext.Current"</strong>?
  am I doing the entire design wrong somehow?</p>
  
  <p>Thanks!</p>