有没有办法模仿 ThreadStatic 与 HttpContext.Current.Items 一起使用?

发布于 2024-11-09 18:00:39 字数 385 浏览 0 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玻璃人 2024-11-16 18:00:39

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文