当我们设置线程 CurrentCulture/CurrentUICulture 时,是否会影响 ASP.NET 中的多个用户?
当我们设置 CurrentCulture 和/或 CurrentUICulture 时,我们在当前线程上执行此操作,如下所示:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
这是否意味着我们可能会影响 Web 应用程序的多个用户的区域性设置,因为他们的请求可能会重用池中的线程?
我正在开发一个 ASP.NET MVC 应用程序,其中每个用户可能在他/她的帐户数据中指定自己的区域性设置。当用户登录时,将从数据库中检索区域性设置,并且必须将其设置为当前区域性。
我担心的是,在当前线程上设置当前区域性可能会影响重用该线程的另一个用户请求。我更担心阅读此内容:
ASP.NET不仅使用线程池,而且还可能在请求处理期间切换线程。
When we set the CurrentCulture and/or CurrentUICulture we do this on the current thread like this:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
Doest this mean we could affect the culture settings of multiple users of our web application as their requests may reuse the threads from pool?
I am working on an ASP.NET MVC application where each user may have own culture setting specified in his/her account data. When the user logs in, the culture setting is retrieved from the database and has to be set as current culture.
My worry is that setting the current culture on the current thread may affect another user request reusing this thread. I am even more concerned reading this:
ASP.NET not only uses a thread pool, but may switch threads during request processing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rocky Lhotka 几年前 在博客中讨论了这个问题 - 并报道 Scott Guthrie 告诉他这一点:
该文章包含更多有趣的材料(他概述了 ASP.NET 切换线程的情况 - 至少截至 2006 年),绝对值得一读。不幸的是,Rocky 没有提供权威来源的链接。 Reflector 在这里也没有多大帮助,因为所有相关方法都在本机调用中终止。
话虽如此,这是有道理的:API 强烈暗示当前的文化是跨上下文切换的,就像当前的主体一样,而且我从未见过 ASP.NET 与预期的行为相矛盾。
Rocky Lhotka blogged about this very question several years ago - and reported that Scott Guthrie told him this:
The article contains more interesting material (he outlines the circumstances in which ASP.NET will switch threads - at least as of 2006), and it's definitely worth reading. Unfortunately, Rocky doesn't provide a link to an authoritative source. Reflector isn't much help here either, since all the relevant methods terminate in native calls.
Having said that, it makes sense: the API strongly implies that the current culture is carried across context switches, like the current principal, and I've never seen ASP.NET contradict the expected behavior.
在标准 ASP.NET 中,我使用了页面字段 UICulture 和 Culture。然后,当请求的不同部分在线程上执行时,线程上下文会发生更改。
话说我不熟悉 MVC 如何管理它,但我认为它应该应用在视图中,并且考虑到视图是从页面继承的视图页面,它们将具有按照 ASP 的 UICulture 和 Culture 。 NET 标准...
说了这么多,不要将此视为福音,正如我所说,我还没有对 ASP MVC 做太多事情...
In standard ASP.NET I used the Page fields UICulture and Culture. Thread contexts are then changed as different parts of the request are executed on the thread.
In saying that I'm not familiar with how MVC manages it, but I'd reckon that it should be applied in the view, and given that views are viewpages which inherit from page, they'll have UICulture and Culture as per ASP.NET standard...
In saying all that, dont take this as gospel, as I say, I haven't done much with ASP MVC...