title: Using HttpContext.Current in WebApi is dangerous because of async
body: <p>My question is a bit related to this: <a href="https://stackoverflow.com/questions/23395632/webapi-equivalent-for-httpcontext-items-with-dependency-injection">WebApi equivalent for HttpContext.Items with Dependency Injection</a>.</p>
  <p><em>We want to inject a class using HttpContext.Current in WebApi area using Ninject.</em></p>
  <p>My concern is, this could be <strong>very dangerous</strong>, as in WebApi (<em>everything?</em>) is async.</p>
  <p>Please correct me if I am wrong in these points, this is what I investigated so far:</p>
  <ol>
  <li><p>HttpContext.Current gets the current context by Thread (I looked into the implementation directly).</p>
  </li>
  <li><p>Using HttpContext.Current inside of async Task is not possible, because it can run on another Thread.</p>
  </li>
  <li><p>WebApi uses IHttpController with method <code>Task&lt;HttpResponseMessage&gt; ExecuteAsync</code> =&gt; every request is async =&gt; you cannot use HttpContext.Current inside of action method. It could even happen, more Request are executed on the same thread by coicidence.</p>
  </li>
  <li><p>For creating controllers with injected stuff into constructors <code>IHttpControllerActivator</code> is used with <strong>sync</strong> method <code>IHttpController Create</code>. This is, where ninject creates Controller with all its dependencies.</p>
  </li>
  </ol>
  <hr />
  <ul>
  <li><p>If I am correct in all of these 4 points, using of <code>HttpContext.Current</code> inside of an action method or any layer below is very dangerous and can have unexpected results. I saw on StackOverflow lot of accepted answers suggesting exactly this. In my opinion this can work for a while, but will fail under load.</p>
  </li>
  <li><p>But when using DI to create a Controller and its dependencies, it is Ok, because this runs on one separated thread. <strong>I could get a value from the HttpContext in the constructor and it would be safe?</strong>. I wonder if each Controller is created on single thread for every request, as this could cause problem under heavy loads, where all threads from IIS could be consumed.</p>
  </li>
  </ul>
  <p><em>Just to explain why I want to inject HttpContext stuff:</em></p>
  <ul>
  <li>one solution would be to get the request in controller action method and pass the needed value all the layers as param until its used somewhere deep in the code.</li>
  <li>our wanted solution: all the layers between are not affected by this, and we can use the injected request somewhere deep in code (e.g. in some <code>ConfigurationProvider</code> which is dependent on URL)</li>
  </ul>
  <p>Please give me your opinion if I am totally wrong or my suggestions are correct, as this theme seems to be very complicated.</p>