C# ASP.net MVC 5多语言:语言资源有时工作不一致

C# ASP.net MVC 5多语言:语言资源有时工作不一致,c#,asp.net,model-view-controller,race-condition,multilingual,C#,Asp.net,Model View Controller,Race Condition,Multilingual,我正在使用资源管理器加载ASP.NET MVC 5应用程序中基于threadUI区域性的资源。我在application_AcquireRequestState事件中设置线程区域性,当前区域性按用户保存,由web服务器在数据库中加载,代码如下: protected void Application_AcquireRequestState(object sender, EventArgs e) { string languageName = default(string);

我正在使用资源管理器加载ASP.NET MVC 5应用程序中基于threadUI区域性的资源。我在application_AcquireRequestState事件中设置线程区域性,当前区域性按用户保存,由web服务器在数据库中加载,代码如下:

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        string languageName = default(string);

        CultureInfo ci = default(CultureInfo);

        if (!User.Identity.IsAuthenticated)
        {
            languageName = Request.RequestContext.HttpContext.Session["_culture"] == null ?
                Helper.ApplicationInformation.AppCulture.Name :
                Request.RequestContext.HttpContext.Session["_culture"].ToString();

            ci = CultureInfo.GetCultureInfo(languageName.ToUpper());
        }
        else
        {
            ci = CultureInfo.GetCultureInfo(Helper.User.Preferences.Language.Name);
        }

        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;

        Bridge.Resources.Global.Culture = ci;
        Bridge.Resources.Login.Culture = ci;
        Bridge.Resources.Search.Culture = ci;
        Bridge.Resources.Workspace.Culture = ci;
    }
它发生在不同线程中设置区域性时,实际上是两个或更多用户同时更改语言时

我认为资源管理器中存在一个RaceCondition问题,该问题导致为当前线程加载具有无效UI区域性的资源

我对此进行了研究,发现了以下相关链接:

我试着下载多语言的例子,但它也发生了, 我从以下链接下载:

在action:index-control:Home中添加“ThreadSleep(4000)”以重现此问题

我做了上面提到的所有事情,但没有做任何工作。 我可以尝试怎样使资源一致地工作


谢谢。

设置每种用户语言有几种方法。 IIS服务器将会话保持20分钟。您需要通过添加行更改web.config文件中的时间:

<sessionState timeout="120" cookieless="AutoDetect">
// Change timeout to your preferred value 
当您使用资源时,请不要忘记一件事,您需要将其放在您的语言短名称的文件名末尾。例如,您有默认文件MainPageLang.resx,并且您需要英语和俄语,您必须添加文件MainPageLang.en.resx和MainPageLang.ru.resx

“用户的数据库值”必须为“en”或“ru”


我尝试使用cookies,但它也有同样的问题,当我在单用户上工作时,它工作得很好,我认为资源管理器中存在一个RaceCondition问题,导致加载资源时当前线程的UI区域性无效!
protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["NAMEOFCOOKIE"];
            if (cookie != null && cookie.Value != null)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
            }
            else
            {
               // FIND IN DB YOUR USER'S LANGUAGE AND SET IT 
                        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                        HttpCookie cookieNew = new HttpCookie("NAMEOFCOOKIE");
                        cookieNew.Value = "YOUR USERS'S DB VALUE";
                        Response.Cookies.Add(cookieNew);
                    }
                }
            }
        }
                    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                    HttpCookie cookieNew = new HttpCookie("NAMEOFCOOKIE");
                    cookieNew.Value = "YOUR USERS'S DB VALUE";
                    Response.Cookies.Add(cookieNew);