Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Web.HttpContext.Current在请求之间是静态的_C#_Asp.net_Request_Static Members - Fatal编程技术网

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

C# System.Web.HttpContext.Current在请求之间是静态的,c#,asp.net,request,static-members,C#,Asp.net,Request,Static Members,在我的web应用程序中,我使用的是System.web.HttpContext.Current,它代表当前的hit上下文,我想知道它是如何从任何地方都可以访问的,直到我注意到它是静态成员! 虽然它是一个静态成员,但如果两个请求几乎同时收到,它如何保持其值。 例如: #Req1----> | set the value of the static field to req1 #Req2----> | set the value of the static field to req2 #

在我的web应用程序中,我使用的是
System.web.HttpContext.Current
,它代表当前的hit上下文,我想知道它是如何从任何地方都可以访问的,直到我注意到它是
静态
成员! 虽然它是一个静态成员,但如果两个请求几乎同时收到,它如何保持其值。 例如:

#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

我是不是错过了什么东西,或者里面有什么把戏?这是一个非常聪明的问题

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

你可以这样想:

[ThreadLocal]
public static HttpContext Current;

是的,这意味着只有主请求线程可以访问它。您启动的其他线程将为空。

先生,非常感谢您的回答!现在一切都清楚多了,但我有一个问题,你认为使用LogicalCallContext实现
是什么意思,再次感谢您非常有用的回答。当您使用Reflector撬开属性时,您会发现该值不是来自静态线程本地字段,而是来自CallContext,反过来又来自LogicalCallContext。它们是我所知甚少的低级基础设施类。它们基本上是每个线程的数据结构(但不完全是)。我认为ASP.NET或WCF中的每个请求都有一个这样的调用上下文,但我可能错了。非常感谢,先生,我将搜索更多关于此主题的内容,但您的答案是完整的!再次感谢。