Is there any way to imitate ThreadStatic for use with HttpContext.Current.Items?

半腔热情 提交于 2020-01-23 02:45:06

问题


Because of Thread Agility in ASP.Net, ThreadStatic is not an appropriate mechanism to use in web applications for segregating static property access from one request to the next.

In order to avoid lots of calls to HttpContext.Current.Items and the associated null checks and so forth, is there any trickery offered by the .Net framework whereby I could create an attribute which works sort of like ThreadStatic, but utilises HttpContext.Current.Items if the current code is being executed within an ASP.Net request context?

note: I am aware that ThreadStaticAttribute is a special case with no functional code internally and is checked for by the JIT compiler before any of its magic is worked.


回答1:


The n2cms project has an AdaptiveContext class built out which you might have a look at. I know there was some talk of doing something similar in log4net, but I'm not sure if that was completed.

Nothing built into the framework, though, as far as I can tell.



来源:https://stackoverflow.com/questions/6122034/is-there-any-way-to-imitate-threadstatic-for-use-with-httpcontext-current-items

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