Is it safe to store an ObjectContext in a thread static variable in ASP.NET?

限于喜欢 提交于 2019-12-05 21:52:57
Heinzi

No, there can be multiple requests in the same thread and, more importantly, one request can be processed in multiple threads. This is called thread agility, and you will run into problems when you store stuff in thread-static variables instead of the Context: When ASP.NET moves from one thread to another during the same request, the HttpContext is still accessible, but the thread-static variable is not.

Some links with further information:

A single thread is used to process a request and no other request will use that thread until the existing request is complete. However you need to consider how you make sure that items in the your context object are disposed of even when there is something exceptional happening. Once the thread is finished with a request for what ever reason it will be re-used to process other requests. You don't want state from a previous request leaking into the next.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!