有没有办法模仿ThreadStatic与HttpContext.Current.Items用?没有办法、ThreadStatic、HttpContext、Current

2023-09-03 15:10:04 作者:做带种的爷们

由于主题敏捷的在ASP.Net, ThreadStatic 不是一个适当的机制在web应用中使用的隔离静态属性访问从一请求到下一​​个。

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.

为了避免大量的电话,以 HttpContext.Current.Items 和相关null检查等等,有没有由.NET Framework提供的任何挂羊头卖狗肉,由此我可以创建一个类的工作方式类似于 ThreadStatic 属性,但使用 HttpContext.Current.Items 如果当前code目前正在一个ASP.Net请求上下文中执行?

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?

注:我知道ThreadStaticAttribute是一个特例,没有功能$ C $内部c和由之前它的任何神奇的是工作的JIT编译器检查的

推荐答案

该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.