System.Web.HttpContext.Current is static between requests

In my web application I use System.Web.HttpContext.Current and it represents the current context, I wonder how it is accessible from the outside until I noticed that its member is static ! Although its a static member, how it retains its meaning, and if two requests are received almost at the same time. eg:

 #Req1----> | set the value of the static field to req1 #Req2----> | set the value of the static field to req2 #Req1 | use that static its supposed to be req2 while its req1 

I missed, understood something or is there a trick in it or what?

+7
source share
1 answer

This is a very clever question!

HttpContext.Current is implemented as a local thread variable. In fact, it is implemented using a LogicalCallContext , but it behaves like a thread-local.

Think of it this way:

 [ThreadLocal] public static HttpContext Current; 

And yes, that means that only the primary request flow can access it. It will be empty for additional threads that you start.

+5
source

All Articles