2011-02-10 16 views
4

我正在考虑使用HttpModule进行本地化(基于this article中的示例) - 但我很好奇,这是否安全?使用HttpModule进行本地化是否安全?

下面的代码,以供参考:

public class CookieLocalizationModule : IHttpModule 
{ 
    public void Dispose() 
    { 
    } 

    public void Init(HttpApplication context) 
    { 
     context.BeginRequest += new EventHandler(context_BeginRequest); 
    } 

    void context_BeginRequest(object sender, EventArgs e) 
    { 
     // eat the cookie (if any) and set the culture 
     if (HttpContext.Current.Request.Cookies["lang"] != null) 
     { 
      HttpCookie cookie = HttpContext.Current.Request.Cookies["lang"]; 
      string lang = cookie.Value; 
      var culture = new System.Globalization.CultureInfo(lang); 
      // is it safe to do this in all situations? 
      Thread.CurrentThread.CurrentCulture = culture; 
      Thread.CurrentThread.CurrentUICulture = culture; 
     } 
    } 
} 

我的印象是,多个线程可能服务的Web请求下。将当前/当前UI文件设置为这样的HttpModule是否安全并且在Web请求的生命周期中是否遵守这些文件是安全的,无论涉及多少个线程服务?

更新:

我已经在生产中使用这种方法了近一年了,所以我肯定能证明,这是完全可以放心使用一个HttpModule本地化。

回答

2

是的,这应该没问题。

我很确定只有一个线程会为请求提供服务,除非你明确地启动另一个线程,并且在这种情况下,文化(和其他事物)被复制到另一个线程。

+0

您能否引用“文化(和其他事物)被复制到另一个线索”的来源? – DanP 2011-02-10 14:10:29

相关问题