有没有办法模仿 ThreadStatic 与 HttpContext.Current.Items 一起使用?
由于 ASP.Net 中的线程敏捷性,ThreadStatic
不是一种适合在 Web 应用程序中用于将静态属性访问从一个请求分离到下一个请求的机制。
为了避免大量调用 HttpContext.Current.Items
以及相关的 null 检查等,.Net 框架是否提供了任何技巧,我可以通过它创建一个类似于ThreadStatic
,但如果当前代码在 ASP.Net 请求上下文中执行,则使用 HttpContext.Current.Items
?
注意:我知道 ThreadStaticAttribute 是一种特殊情况,内部没有功能代码,并且在其任何魔力发挥作用之前由 JIT 编译器进行检查。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
n2cms 项目有一个 AdaptiveContext 类 构建出来,您可以看看。我知道有人在谈论在 log4net 中做类似的事情,但我不确定这是否已经完成。
不过,据我所知,框架中没有内置任何内容。
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.