In my web application I'm using System.Web.HttpContext.Current
and it represents the current hit context, I was wondering how its accessible from everywhere until i noticed that its a static
member !
While its a static member how it keeps its value while if two requests received in almost the same time.
like the following :
#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
did I miss-understand something or there is a trick in it or what ?
The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.
Current. Item” data is live for single HTTP request/Response where HttpContext. Current. Session data is live throughout user's session.
HttpContext isn't thread-safe. Reading or writing properties of the HttpContext outside of processing a request can result in a NullReferenceException.
HttpContext.Current.Request.ServerVariables("LOGON_USER") Request.ServerVariables("LOGON_USER") it will work only when Windows Integrated Authentication is turned on and Anonymous. Access is turned off. in this case, the Request.ServerVariables("LOGON_USER") will return the network.
This is a very intelligent question!
HttpContext.Current
is implemented as a thread-local variable. Actually, it is implemented using LogicalCallContext
but that behaves like a thread-local.
Think of it like this:
[ThreadLocal]
public static HttpContext Current;
And yes, this means that only the primary request thread can access it. It will be null on additional threads that you start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With