做ASP.NET请求始终的BeginRequest和EndRequest在同一个线程?线程、在同一个、NET、ASP

2023-09-03 04:08:34 作者:空叹花语意

的BeginRequest和EndRequest为ASP.NET的HttpApplication是否总是发生在完全相同的线程来自客户端的给定的HTTP请求?

我想问的原因是,我看到了一些很奇怪的现象,其中一个ThreadStatic变量不是一个init方法为IHttpModule的内空。

我设置ThreadStatic变量对EndRequest上的BeginRequest值和空它。

不过,我IHttpModule的Init方法应在的BeginRequest / EndRequest期间外调用,所以唯一的办法我能想到的,这ThreadStatic变量将有一个当我的初始化方法被调用值,如果EndRequest发生在不同的线程比的BeginRequest,因此该值仍没有后来当ASP .NET尝试使用相同的线程创建一个新的HttpApplication实例上的空...

我在集成模式下运行IIS 7。

解决方案   

请问的BeginRequest和EndRequest一个ASP .NET的HttpApplication总是发生在完全相同的线程来自客户端的给定的HTTP请求?

没有。有选择,在通向请求结束的请求在不同的线程正在处理执行异步操作。这是不正常的情况下

请参阅 @Page 指令的异步属性:的 http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx

AIoT安全峰会议题回顾 车联网安全的攻与防

有关介绍如何使用异步页,看到这个MSDN杂志文章:异步在ASP.NET 2.0 的页面。

Does BeginRequest and EndRequest for an ASP.NET HttpApplication always occur on the exact same thread for a given HTTP request from a client?

The reason I ask is that I am seeing some very strange behavior where a ThreadStatic variable is not null inside of an Init method for an IHttpModule.

I set this ThreadStatic variable to a value on BeginRequest and null it on EndRequest.

However, my IHttpModule Init method should be called outside of the BeginRequest/EndRequest period, so the only way I can think of that this ThreadStatic variable would have a value when my Init method is called is if EndRequest occurs on a different thread than BeginRequest and thus the value is still not null later on when ASP .NET tries to use that same thread to create a new HttpApplication instance...

I am running IIS 7 in integrated mode.

解决方案

Does BeginRequest and EndRequest for an ASP .NET HttpApplication always occur on the exact same thread for a given HTTP request from a client?

No. There are options to perform asynchronous operations in the request leading to the end of the request being handled on a different thread. This is not the normal case.

See the Async attribute of @Page directive: http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx

For an introduction to using asynchronous pages, see this MSDN Magazine article: "Asynchronous Pages in ASP.NET 2.0".