Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting current culture with threads in ASP.NET MVC

Here's an example of SetCulture attribute which inside does something like this:

    public void OnActionExecuting(ActionExecutingContext
        filterContext)
    {
        string cultureCode = SetCurrentLanguage(filterContext);

        if (string.IsNullOrEmpty(cultureCode)) return;

        HttpContext.Current.Response.Cookies.Add(
            new HttpCookie("Culture", cultureCode)
            {
                HttpOnly = true,
                Expires = DateTime.Now.AddYears(100)
            }
        );

        filterContext.HttpContext.Session["Culture"] = cultureCode;

        CultureInfo culture = new CultureInfo(cultureCode);
        System.Threading.Thread.CurrentThread.CurrentCulture =
            culture;
        System.Threading.Thread.CurrentThread.CurrentUICulture =
            culture;
    }

I was wondering how does this affect a site with multiple users logged on and each one setting their own culture? What is the scope of a thread here with regards to the IIS worker process (w3wp) that the site is running in?

like image 556
mare Avatar asked Feb 22 '11 15:02

mare


People also ask

How do you change the current UI culture?

To change the current UI culture, you assign the CultureInfo object that represents the new UI culture to the Thread. CurrentThread. CurrentUICulture property.

What is UICulture?

UICulture affects which resource file (Resources. lang. resx) is going to be loaded to by your application. So to load German resources (presumably localized text) you would set UICulture to the German culture and to display German formatting (without any impact on which resources are loaded) you would set Culture .


1 Answers

it's exactly the same as with normal Asp.Net. The thread is used for this request from start to finish, and then effectively thrown away (if you want to be pedantic the underlying platform thread sticks around for a while).

So multiple users will not be affected - since each one gets their own concurrent thread - I'm doing exactly the same thing on a few sites (including one that regularly gets hit with tens of thousands unique visitors in the space of a couple of hours) and its always fine.

like image 170
Andras Zoltan Avatar answered Nov 08 '22 14:11

Andras Zoltan