Thread.CurrentThread issue in n-tier application

Last post 03-27-2008 10:21 AM by s.sriram. 6 replies.

Sort Posts:

  • Thread.CurrentThread issue in n-tier application

    03-24-2008, 10:29 AM
    • Loading...
    • Juse71
    • Joined on 03-24-2008, 1:46 PM
    • Posts 3

    Hi, I'm having a problem with a n-tier ASP.NET with italian and english language user interface.

    In the UI layer, accordingly with the user decision, I override the InitializeCulture method to set the following:

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("it-IT");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("it-IT");
    or 
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

    All is fine in the UI layer and when execution passes to the business layer the setting is still ok, but when execution steps into the very first line of a method in the Data Access Layer the CurrentCulture and CurrentUICulture are always set automatically to it-IT. And I saw that even the ManagedThreadId property changes.

    I can't understand why, there is something that seems to reset language to it-IT, but just in the Data Access Layer....

    This happens even if I don't setup anything in the web.config, and if I set my browser language to something different from italian.

    What's the problem with the thread? How can I solve it?
    Has anyone experienced something similar or can help as I'm stuck on this point?

    Thanks

    Juse

     

  • Re: Thread.CurrentThread issue in n-tier application

    03-24-2008, 12:56 PM
    • Loading...
    • F16I
    • Joined on 03-03-2006, 1:09 AM
    • New York
    • Posts 174

    I don't know whats the problem.
    but I think that the DAL layer shouldn't know (anything) about the UI layer.

    The UI layer should send that I want this language and it should get that.

  • Re: Thread.CurrentThread issue in n-tier application

    03-26-2008, 8:27 AM
    • Loading...
    • s.sriram
    • Joined on 02-27-2007, 3:38 AM
    • India
    • Posts 42

    Hi,

    Once the user select his language and on some event (ie)button click or something, store the selected language from dropdown to a session variable.

    HttpContext.Current.Session["Culture"] = ddlLanguage.SelectedItem.Value;

    And add code below in Global.asax 

     

    //Code to be placed in Global.asax

    protected void Application_AcquireRequestState(

    object sender, EventArgs e)

    {

     string culture = System.Globalization.CultureInfo.CurrentCulture.Name;
            if (HttpContext.Current.Session != null &&  HttpContext.Current.Session["Culture"] != null)
            {
                culture = Convert.ToString(HttpContext.Current.Session["Culture"]);
            }
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(culture);
            System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
            System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;

     }

     

    The above code worked for me, i have master pages used in my application, and by this u can have users select their preferred language. The user selected language will be applied through out the application. Offcourse i assume u have the proper resource file created for different language. 

    Hope this helps, Let me know if u still have any problem.

    S.SRIRAM
  • Re: Thread.CurrentThread issue in n-tier application

    03-27-2008, 4:51 AM
    • Loading...
    • Juse71
    • Joined on 03-24-2008, 1:46 PM
    • Posts 3

    Great! Thank you very much!

    I didn't know the Application_AcquireRequestState method.
    So I guess you had experienced the same issue... still I can't undersand the reason the thread changed just inside the DAL layer.

    This approach, I mean to retrieve the Thread.CurrentThread.CurrentCulture in each layer you need it, seems to be "lighter" rather than having to add the language as a parameter in each method, throughout all the layers. What do you think?

    Thanks again :-)

  • Re: Thread.CurrentThread issue in n-tier application

    03-27-2008, 7:01 AM
    • Loading...
    • s.sriram
    • Joined on 02-27-2007, 3:38 AM
    • India
    • Posts 42

    Hi,

    The above code which i wrote will make your application localizable.

    That will pick the values from the resource files based on the language that the user selects. Your resources files will contain the corresponding values (For Example) If you have to support both German and English language, you will have 2 resource files one will be Sample.de.resx - for German and Sample.resx for English (assuming English as default). So your German resource file will have german values with some special characters, and your English resource file or the defult one contains your
    normal values.

    whenever user change the language, you will find all the controls like label's, button's Text are showing values from the resource file.

    In your query u mentioned abt threads changed in DAL layer. what is need for changing thread in DAL layer?...

    Hope i got yur query right. Please let me know if you have any issues.

     

    S.SRIRAM
  • Re: Thread.CurrentThread issue in n-tier application

    03-27-2008, 8:01 AM
    • Loading...
    • Juse71
    • Joined on 03-24-2008, 1:46 PM
    • Posts 3

    The matter was that my multilingual terms are on an existing MySQL database, so I'm not using any of the ASP.NET resources, such as .resx files (which I know would be assolutely better!).

    The problem I had encountered in this n-tier application was that the thread strangely got changed whenever the execution stepped into the DAL layer.
    In fact, in my DAL code I didn't change the thread myself, I just used it to retrieve the culture like this:

    String language = Thread.CurrentThread.CurrentCulture.ThreeLetterISOLanguageName;

    Upon the value of this language variable then I query the MySQL database to get only the records which have value 'ITA' (or 'ENG').

    Now with your code it is ok, it works fine!, the thread is forced to keep the value of the session variable.

    By the way, do you know what the Application_AcquireRequestState method is meant for exactly? I noticed it is executed repeatedly during execution.

    Thank you

     

  • Re: Thread.CurrentThread issue in n-tier application

    03-27-2008, 10:21 AM
    • Loading...
    • s.sriram
    • Joined on 02-27-2007, 3:38 AM
    • India
    • Posts 42

    Hi ,

    Application_AcquireRequestState:     Fired when the ASP.NET page framework gets the current state (Session state) related to the current request.

     

    S.SRIRAM
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter