6

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?

mare
  • 13,033
  • 24
  • 102
  • 191

2 Answers2

5

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.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • 1
    The thread is in a thread pool, so it's actually re-used rather than thrown away. So long as you're always setting the culture on every request e.g. by using a global filter, it'll be the same to the users though. – zcrar70 Aug 22 '11 at 15:09
0

I dispute the accepted answer based on the articles below. Changing the culture of the current thread assumes there is a similar lifecycle between threads and requests which does not seem to be the case, at least not for all scenarios. So you may or may not have a problem doing it. Please check the articles below.

asp.net mvc3 request thread affinity

http://forum.springframework.net/showthread.php?572-CallContext-vs.-ThreadStatic-vs.-HttpContext&highlight=LogicalThreadContext

http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx

http://www.hanselman.com/blog/ATaleOfTwoTechniquesTheThreadStaticAttributeAndSystemWebHttpContextCurrentItems.aspx

Community
  • 1
  • 1