System.Web.HttpContext.Current在请求之间是静态的

Hil*_*lmi 7 c# asp.net request static-members

在我正在使用的Web应用程序中System.Web.HttpContext.Current,它代表了当前的热门环境,我想知道它是如何从各处访问的,直到我注意到它是一个static成员!虽然它是一个静态成员,但如果在几乎同一时间内收到两个请求,它如何保持其价值.如下:

#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
Run Code Online (Sandbox Code Playgroud)

我错过了什么或者有什么诀窍或什么?

usr*_*usr 5

这是一个非常聪明的问题!

HttpContext.Current实现为线程局部变量.实际上,它是使用LogicalCallContext但是行为类似于线程本地.

可以这样想:

[ThreadLocal]
public static HttpContext Current;
Run Code Online (Sandbox Code Playgroud)

是的,这意味着只有主请求线程才能访问它.它将在您启动的其他线程上为null.