IIS的单个Web请求是否保留在单个线程上? [英] Does a single web request to IIS stay on a single thread?
问题描述
我想编写一个日志记录http模块,该模块在请求执行时为线程本地存储中的单个请求存储日志事件列表。在End_Request上我想将所有事件写回持久存储。
I want to write a logging http module that stores a list of log events for a single request in thread local storage while the request executes. On End_Request I want to write all the events back to persistent storage.
问题是,一个请求是否与一个线程匹配?即我可以在我的代码中的任何地方假设我可以将项目添加到IEnumerable中,并且它们将在请求结束时正确地组合在一起。
Question is, will one request match to one thread? I.e. can I assume from anywhere in my code that I can add items to the IEnumerable and they will properly be all together at the end of the request.
推荐答案
没有。 ASP.NET可以在处理请求时切换线程。这被称为线程敏捷性。
No. ASP.NET can potentially switch threads while processing a request. This is known as thread-agility.
只有某些点可以/将会这样做。我不记得他们是什么了 - 现在正在寻找参考...
There are only certain points where it can/will do this. I can't remember what they are off the top of my head - searching for a reference now...
但简短的回答是 NO :您不能依赖于整个请求期间可访问的同一线程本地存储。
But the short answer is NO: you can't rely on the same thread-local storage being accessible for the entire duration of the request.
这篇关于IIS的单个Web请求是否保留在单个线程上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!