Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we affect multiple users in ASP.NET when we set the Thread CurrentCulture/CurentUICulture?

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.

like image 912
Nikolay Avatar asked May 19 '10 07:05

Nikolay


1 Answers

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.

like image 97
Jeff Sternal Avatar answered Sep 19 '22 15:09

Jeff Sternal