Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.HttpContext.Current is static between requests

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 ?

like image 885
Hilmi Avatar asked Mar 23 '13 11:03

Hilmi


People also ask

How does HttpContext current work?

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.

What is the difference between HttpContext current items and HttpContext current session in asp net?

Current. Item” data is live for single HTTP request/Response where HttpContext. Current. Session data is live throughout user's session.

Is HttpContext current items thread safe?

HttpContext isn't thread-safe. Reading or writing properties of the HttpContext outside of processing a request can result in a NullReferenceException.

What is HttpContext current request ServerVariables?

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.


1 Answers

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.

like image 148
usr Avatar answered Sep 18 '22 10:09

usr