Do ASP.NET Requests always BeginRequest and EndRequest on the same thread?

后端 未结 3 1313
暖寄归人
暖寄归人 2021-01-14 09:48

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 tha

3条回答
  •  忘掉有多难
    2021-01-14 10:15

    No, as per Richard's answer.

    As an aside, you're supposed to "share" variables between HTTP Modules via HttpContext.Current.Items. In the case of [ThreadStatic], due to thread agility in ASP.NET it's not guaranteed that your code will start and finish on the same thread. So, ThreadStatic isn't such a great idea in ASP.NET.

    It's worth saying that EndRequest will always execute with the same HttpContext.

    This seems to be the definitive post on using [ThreadStatic] verses HttpContext http://piers7.blogspot.co.nz/2005/11/threadstatic-callcontext-and_02.html

    Scott Hanselman also posted on the subject:
    http://www.hanselman.com/blog/ATaleOfTwoTechniquesTheThreadStaticAttributeAndSystemWebHttpContextCurrentItems.aspx

    And Jon Skeet has a good SO answer here too:
    CallContext vs ThreadStatic

提交回复
热议问题