如何在ASP.NET Web服务调用中动态初始化区域性?

如何在ASP.NET Web服务调用中动态初始化区域性?,asp.net,web-services,currentculture,Asp.net,Web Services,Currentculture,有人能告诉我如何在asp.net Web服务调用中动态初始化线程区域性吗 在我的aspx页面中,我有一个基页,我在其中重写InitializeCulture()方法 请注意,所选语言的值保存在会话状态。在Global.asax文件中,您可以设置当前区域性,即使是其web服务或网页 // PreRequestHandlerExecute occurs after initialization of Session void Application_PreRequestHandlerExecute(

有人能告诉我如何在asp.net Web服务调用中动态初始化线程区域性吗

在我的aspx页面中,我有一个基页,我在其中重写InitializeCulture()方法


请注意,所选语言的值保存在会话状态。

Global.asax
文件中,您可以设置当前区域性,即使是其web服务或网页

// PreRequestHandlerExecute occurs after initialization of Session
void Application_PreRequestHandlerExecute(Object Sender, EventArgs e)
{
    // check if session is required for the request
    // as .css don't require session and accessing session will throw exception
    if (Context.Handler is IRequiresSessionState
        || Context.Handler is IReadOnlySessionState)
    {
        string culture = "en-US";
        if (Session["MyCurrentCulutre"] != null)
        {
            culture = Session["MyCurrentCulutre"] as String;
        }

        System.Threading.Thread.CurrentThread.CurrentCulture = 
           System.Globalization.CultureInfo.CreateSpecificCulture(culture);
    }
}
您正在更改需求,但是
会话
对象在
开始请求
方法中不可用,您可以在web方法中执行此操作。

[WebMethod]
public static string MyWebMethod()
{
    String culture = Session["MyCurrentCulutre"] as String;

    System.Threading.Thread.CurrentThread.CurrentCulture = 
       System.Globalization.CultureInfo.CreateSpecificCulture(culture);

    return "My results";
}

InitializeCulture()是一个页面方法,而不是System.Web.Services.WebService方法。@Waqas Raja,你能给我一个现实生活中的免费例子吗?我为什么要这么做?假设我来自以色列,iis在美国……你能举个例子吗?想象一下我在会话中保存了首选的用户语言。然后,我的javascript发出一个webservice调用。在响应中,我想用用户首选语言发送一条用户消息…@WaqasRaja您所说的是正确的,但所选语言的值处于会话状态,并且在应用程序中_BeginRequest会话尚未加载。这是我的解决方案,但我试图在我创建的每个webmethod中避免这样做。