2012-08-16 56 views
1

這裏是我的代碼託管..傳遞文化價值由IIS

private IHelloWorld ChannelFactoryWebService() 
     { 
      ServiceEndpoint tcpEndPoint = new ServiceEndpoint(
               ContractDescription.GetContract(typeof(IHelloWorld)), 
               new NetTcpBinding(), 
               new EndpointAddress("net.tcp://myserver/CultureTest/Service.svc")); 

     ChannelFactory<IHelloWorld> factory = new ChannelFactory<IHelloWorld>(tcpEndPoint); 
     factory.Endpoint.Behaviors.Add(new CultureBehaviour()); 

     return factory.CreateChannel(); 
    } 


    [TestMethod] <-------------- FAILING TEST ---- 
    public void ChangingCultureWASViaEndPointTest() 
    { 
     IHelloWorld client = ChannelFactoryWebService(); 

     CultureInfo cultureInfo = new CultureInfo("ar-SA"); 
     Thread.CurrentThread.CurrentCulture = cultureInfo; 

     client.Hello(); 
     string culture = client.HelloCulture(); 

     Assert.AreEqual("ar-SA", culture); <--- fails here.. I get en-US for culture 

    } 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)] 
public class Server : IHelloWorld 
{ 
    #region IHelloWorld Members 

    public void Hello() 
    { 
     Console.WriteLine(string.Format("Hello world from the server in culture {0}", Thread.CurrentThread.CurrentCulture.Name)); 
    } 

    public string HelloCulture() 
    { 

     string cultureName = Thread.CurrentThread.CurrentCulture.Name; 
     return cultureName; 
    } 

    #endregion 
} 


[ServiceContract] 
public interface IHelloWorld 
{ 
    [OperationContract] 
    void Hello(); 

    [OperationContract] 
    string HelloCulture(); 
} 


    public class CultureMessageInspector : IClientMessageInspector, IDispatchMessageInspector 
    { 
     private const string HeaderKey = "culture"; 
     #region IDispatchMessageInspector Members 

     public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) 
     { 
      int headerIndex = request.Headers.FindHeader(HeaderKey, string.Empty); 
      if (headerIndex != -1) 
      { 
       Thread.CurrentThread.CurrentCulture = new CultureInfo(request.Headers.GetHeader<String>(headerIndex)); 
      } 
      return null; 
     } 

     public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState) 
     { 
     } 

     #endregion 

     #region IClientMessageInspector Members 

     public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) 
     { 
     } 

     public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) 
     { 
      request.Headers.Add(MessageHeader.CreateHeader(HeaderKey, string.Empty, Thread.CurrentThread.CurrentCulture.Name)); 
      return null; 
     } 

     #endregion 
    } 


    public class CultureBehaviour : IEndpointBehavior 
    { 
     #region IEndpointBehavior Members 

     public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
     { 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime) 
     { 
      CultureMessageInspector inspector = new CultureMessageInspector(); 
      clientRuntime.MessageInspectors.Add(inspector); 
     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) 
     { 
      CultureMessageInspector inspector = new CultureMessageInspector(); 
      endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector); 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 
     } 

     #endregion 
    } 


public class CultureBehaviorExtension : BehaviorExtensionElement 
{ 
    // BehaviorExtensionElement members 
    public override Type BehaviorType 
    { 
     get { return typeof(CultureBehaviour); } 
    } 

    protected override object CreateBehavior() 
    { 
     return new CultureBehaviour(); 
    } 
} 

的Web配置的託管網站的服務如下所示:

<endpointBehaviors> 
    <behavior name="Default"> 
     <CultureExtension/> 
    </behavior> 
    </endpointBehaviors> 

    <serviceBehaviors> 
    <behavior> 
     <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
     <serviceMetadata httpGetEnabled="true"/> 
     <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

<extensions> 
    <behaviorExtensions> 
    <add name="CultureExtension" type="Extension.CultureBehaviorExtension, Extension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
    </behaviorExtensions> 
</extensions> 

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

我的問題是,下面的代碼裏面,當我通過我無法得到服務的Hello方法中的AR-SA的文化價值的網站, 託管服務。 我盡力在這裏澄清問題。請讓我知道還有什麼需要澄清的。

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)] 
public class Server : IHelloWorld 
{ 
    #region IHelloWorld Members 

    public void Hello() 
    { 
    <--- problem here unable to get culture value of ar-SA here ---> 
     Console.WriteLine(string.Format("Hello world from the server in culture {0}", Thread.CurrentThread.CurrentCulture.Name)); 
    } 

    public string HelloCulture() 
    { 

     string cultureName = Thread.CurrentThread.CurrentCulture.Name; 
     Console.WriteLine("**************************hello**********************************"); 
     Console.WriteLine(cultureName); 
     return cultureName; 
    } 

    #endregion 
} 
+0

+1。對不起,我沒有試圖回答你的問題。我正在運行自託管的WCF服務,並且您的代碼將我設置在正確的路徑上以添加文化行爲。 – 2014-11-11 18:15:09

回答

1

它不是一個真正的答案,爲什麼特定的代碼不工作,但你爲什麼不只是在文化字符串中發送,並設置它在你的方法對WCF服務?

這就是我們usualy做到這一點,或者如果它通過身份驗證的用戶,只需將其保存到用戶,使他們可以切換到任何語言,無論電腦配置的。

+0

我們不想通過方法調用發送的文化。順便說一句,當我使用控制檯應用程序託管服務時,此代碼有效。它不支持iis託管的服務。 :( – 2012-08-16 21:12:09