title: Access to HttpContext via static class works "correctly" with different requests
body: <p>I found <a href="https://www.quickdevnotes.com/better-approach-to-use-httpcontext-outside-a-controller-in-net-core-2-1/" rel="nofollow noreferrer">this article</a> while trying to solve a problem that requires some headers in non-controllers.<br>
  I am quite skeptical about the approach and the author is not responding. My major concern is about the approach of having global static <code>HttpContext</code>. I was thinking that it <strong>should not</strong> work with two requests. An example of this case is below (together with the approach presented in the article I mentioned):  </p>
  
  <pre><code>public static class AppContext
  {
      public static IHttpContextAccessor HttpContextAccessor { get; set; }
      public static void Configure(IHttpContextAccessor accessor)
      {
          HttpContextAccessor = accessor;
      }
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env,
      IHttpContextAccessor contextAccessor)
  {
      AppContext.Configure(contextAccessor);
      ...
  }
  
  [Route("api/[controller]")]
  [ApiController]
  public class ExampleController : ControllerBase
  {
      [HttpGet("{number}")]
      public IActionResult Example(int number)
      {
          if (number == 1)
          {
              Thread.Sleep(10000);
          }
  
          var result = AppContext.HttpContextAccessor.HttpContext.Request.GetDisplayUrl();
  
          return Ok(result + " " + number);
      }
  }
  </code></pre>
  
  <p>Want to mention that author uses name <strong>App</strong>Context for this static class and that is exactly what I'd expect (and it is indeed useless then).<br>
  However, what confuses me is the actual behavior. I am debugging the piece placing a breakpoint at the line with <code>var result = ...</code>. I first send a request with <code>number</code> = 1 that will sleep for a bit and then I send the second request with a different value for <code>number</code>. I skip the placed breakpoint for the first request and wait for the first request (with <code>number</code> = 1) to stop there. Then I check what <code>GetDisplayUrl()</code> returns - and it returns a path with <code>/1</code> (that is indeed the path for this request that has slept for 10 secs). I would expect it to end with <code>/2</code> as the static field with <code>IHttpContextAccessor</code> of static class <code>AppContext</code> has been rewritten by the second request in <code>ConfigureServices()</code> method.<br>
  I believe I am missing something crucial and would be glad if you also provided some sources that I (and others confused) could use to fill the gap.<br>
  Could you also give me some more insights on using the approach? Does testability suffer (as I'm using the static class everywhere in the application) and in what way?</p>