ThreadStaticAttribute在ASP.NET [英] ThreadStaticAttribute in ASP.NET

查看:113
本文介绍了ThreadStaticAttribute在ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要存储静态值前每个线程的组件。它的,可以在许多情况下不仅在ASP.NET中使用的一般组件。

I have a component that needs to store static values fore each thread. It's a general component that can be used in many scenarios and not only in ASP.NET.

我想使用 [ThreadStatic] 属性来实现我的目标。假设这也将正常工作在ASP.NET中的场景,因为我是假设每个请求被称为在自己的线程。

I was thinking to use the [ThreadStatic] attribute to achieve my goal. Supposing that it would also work fine in ASP.NET scenarios, because i was assuming that every Request is called in a own thread.

经过一番研究,我发现,<一个href="http://www.hanselman.com/blog/ATaleOfTwoTechniquesTheThreadStaticAttributeAndSystemWebHttpContextCurrentItems.aspx">this博客文章在Scott Hanselman 的话说,当ASP.NET中使用 [ThreadStatic] 要小心。

After some research i found this Blog Post from Scott Hanselman saying to be careful when using [ThreadStatic] in ASP.NET.

然而,大多数的意见(以下邮政)不同意,这是斯科特写道,他说,一个请求总是在一个线程和线程不使用在同一时间的另一个要求运行。这也是我所相信的,但很想有关于你的一些专家的意见在这里。

However most of the comments (below the Post) do not agree with that was Scott wrote, saying that a Request always run in one thread and that the thread is not used by another request at the same time. That's also what I believe but would love to have some opinion about you experts in here.

推荐答案

不,斯科特是正确的:要求绝对的的必须在其整个过程一个线程中运行。 ASP.NET是的线程敏捷的在这方面。目前只有几个点,其中开关可能发生,但它肯定的可以的发生。 (我已经为自己进行了测试。)

Nope, Scott is right: a request definitely doesn't have to run on a single thread for its entire duration. ASP.NET is thread-agile in that respect. There are only a few points where the switch can happen, but it definitely can happen. (I've tested it for myself.)

您不妨读一读这个博客帖子和的今年春季论坛主题的一些细节

You may wish to read this blog post and this Spring forum thread for some more details.

基本上,你会发现捕捉范围内以不同的方式。从您的角度来看的相关位可能是在博客文章的末尾:

Basically you should find a different way of capturing context. The relevant bit from your point of view is probably at the end of the blog post:

这是一个重大的皮塔饼,因为据我可以看到它意味着在ASP.Net'ThreadStatic'esque行为的唯一持久性的选择是使用HttpContext的。因此,对于你的业务对象,无论是你坚持的,如果(HttpContext.Current!= NULL)和System.Web程序引用(呸),或者你一定要拿出一些提供者模型的静态持久性,这将需要事先设置到任何这些单身的被访问点。双呸。

This is a major PITA, because as far as I can see it mean the only persistence option for 'ThreadStatic'esque behavior in ASP.Net is to use HttpContext. So for your business objects, either you're stuck with the if(HttpContext.Current!=null) and the System.Web reference (yuck) or you've got to come up with some kind of provider model for your static persistence, which will need setting up prior to the point that any of these singletons are accessed. Double yuck.

这篇关于ThreadStaticAttribute在ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆