ASP.Net MVC资源文件有时会错误地将ResouceManager装 [英] ASP.Net MVC resource files are sometimes incorrectly loaded by the ResouceManager

查看:116
本文介绍了ASP.Net MVC资源文件有时会错误地将ResouceManager装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个跨国的网站已经本地化的内容为它服务的各个国家。这个定位是使用标准的.NET资源文件执行。

We have a multinational website which has localised content for the various countries it serves. This localisation is implemented using standard .Net resource files.

当我们的web应用程序启动时,或对生产环境负荷回收,有时它会显示错误的资源为特定的国家。例如。英国网站可能显示法语内容。

When our web application starts up or recycles under load on the production environment, sometimes it will display the wrong resources for a particular country. E.g. the UK site may show French content.

这将继续发生,直到重新启动应用程序。

This continues to happen until the application is restarted.

生产环境是Windows Server 2012中的IIS上的8。
该应用程序是在ASP.Net MVC 4实现的。

The production environment is IIS 8 on Windows Server 2012. The application is implemented in ASP.Net MVC 4.

应用程序决定哪个区域是由输入网址服务。所以www.mysite.com将是英国英语www.mysite.fr将法国等。

The application decides which locale it is serving by the incoming URL. So www.mysite.com will be UK English www.mysite.fr will be French etc.

我们有其经由一个Web.config注册IHttpModule的的实现。
在模块的init方法,它附加一个处理程序的BeginRequest事件。
在该方法中,传入的URL被检查和该线程的CurrentUICulture被设定为适当的值。 EN-GB为www.mysite.com,FR-FR为www.mysite.fr等。

We have an implementation of IHttpModule which is registered via a Web.config. In the Init method of the module, it attaches a handler to the BeginRequest event. In this method, the incoming URL is examined and the thread's CurrentUICulture is set to an appropriate value. en-GB for www.mysite.com, fr-FR for www.mysite.fr etc.

此系统工作良好的大部分。但是,有时当应用程序启动了,而它收到请求,将始终如一地服务于错误的内容对一些资源文件。

This system works well for the most part. However, sometimes when the application starts up while it is receiving requests, it will consistently serve the wrong content for some of the resource files.

会继续,直到重新启动应用程序做到这一点。它可能会再次重新启动服务不正确的内容。我们必须保持重新启动,直到它的服务对象是正确的内容,在这一点上,它会保持稳定。

It continues to do this until the application is restarted. It may again restart serving the incorrect content. We have to keep restarting until it is serving the correct content, at which point it will remain stable.

我们已经能够通过(使用招)启动过程中在应用投掷请求本地重现此开发PC上。该网站显示了在英国版本的网站的一些资源文件内容德

We have been able to reproduce this locally on a development PC by throwing requests at the application during startup (using Fiddler). The site was showing German content for some resource files on the UK version of the site.

检查过我们的code明显的罪魁祸首(该的CurrentUICulture由HTTP模块设置正确并保持整个请求的处理是正确的),我们就开始看资源管理器。

Having checked the obvious culprits in our code (that the CurrentUICulture is set correctly by the HTTP module and remains correct throughout the processing of the request), we started to look at the resource manager.

随着应用程序在这种不正确的状态开始,我们考察了ResourceManager类的_resourceSets属性的内容。
这是键控的ISO文化code字典。
检查的EN-GB,我们发现,它没有实际包含从资源文件的德语版的资源字符串的内容。

看起来,有时候,当网站启动时同时接收请求,ResourceManager类加载错误的资源文件一种文化,或者错误地归类在其字典文件。

It seems that sometimes, when the site is starting up while receiving requests, the ResourceManager class is loading the wrong resource file for a culture or it is incorrectly classifying the file in its Dictionary.

有其他人遇到这种行为,是任何人都知道任何变通办法?

Has anyone else experienced this kind of behavior and is anyone aware of any workarounds?

感谢。

推荐答案

这听起来像你在你的处理器线程安全问题。当您修改当前线程的文化,要修改它是可以处理多个请求当前线程。当产生反应,另一个请求可能已经改变了线程当前的语言让所有响应相同的语言。

This sounds like you are having thread safety problems in your handler. When you modify the threads current culture, you are modifying it for the current thread that could be processing multiple requests. When the responses are generated, another request could have altered the thread current language giving all responses the same language.

我有几个建议,你可以开始:

I have a few suggestions you can start off with:


  1. 确保您的处理程序是不可重用。我假设你所做实施的IHttpHandler,为IsReusable属性返回false,因为多个线程应该在同一时间打它一个新的实例将每个请求被创建。

  2. 请不要使用一个处理程序...处理程序是不是这样的事情了理想的解决方案,该preferred地方设置线程文化是<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpapplication.acquirerequeststate.aspx\">Application_AcquireRequestState这将是正确的解雇为每个请求不重叠。

  3. 使用路由处理器来代替:<一href=\"http://adamyan.blogspot.com/2010/07/addition-to-aspnet-mvc-localization.html\">http://adamyan.blogspot.com/2010/07/addition-to-aspnet-mvc-localization.html

  1. Make sure your handler is NOT reusable. I assume you've made implemented IHttpHandler, return false for the IsReusable property since multiple threads should hit it at the same time and a new instance will be created per request.
  2. Don't use a handler... A handler is not the ideal solution for something like this, The preferred place to set the thread culture is in Application_AcquireRequestState which will be correctly fired for each request without overlapping.
  3. Use a routing handler instead: http://adamyan.blogspot.com/2010/07/addition-to-aspnet-mvc-localization.html

在没有看到你说的其他的处理只能是猜测,为什么响应线程共享的文化。这个问题可以很容易地躺在你正在使用以及其本质上是不是线程安全的字典。

Without seeing the handler you speak of the rest can only be speculation as to why responses are sharing thread cultures. The problem could easily lie in the dictionary you are using as well which is inherently not thread safe.

这篇关于ASP.Net MVC资源文件有时会错误地将ResouceManager装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆