views:

50

answers:

2

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.

A: 

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

Overflow
+2  A: 

Rocky Lhotka blogged about this very question several years ago - and reported that Scott Guthrie told him this:

ASP.NET ensures that, even if they switch your thread, the CurrentPrincipal and culture properties from the original thread carry forward to the new thread. This is automatic, and you don’t need to worry about losing those values. Whew!

However, thread-local storage doesn’t carry forward.

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.

Jeff Sternal
Thanks for the link to this blog post. It is really an interesting one and clears the second part of my worries - the thread switching happens during a single request only when an asynch operation has to be performed, so at least this is not random and as the culture is carried forward, it should not be a problem. What remains is the resuing of threads from the pool when a new request arrives, but if we consider that we set the culture for each request in OnActionExecuting then problably this is not a problem as well.
Nikolay