WCF 语言 Header

WCF Language Header

我有一个 WCF 服务,我需要接收一个语言代码(英语为 EN,德语为 DE,意大利语为 IT)以使我的 WCF 服务的所有方法提供的结果国际化。下面是我的 WCF 服务和方法以及我使用的客户端的简化版本。我需要根据从请求的 header 中检索到的语言代码来翻译该方法的结果。

 //IMyService
 [ServiceContract, XmlSerializerFormat]
 public interface IMyService
 {
   [OperationContract]
   string MyFunction(string myParameter);
 }


 //MyService
 public class MyService : IMyService
 {
     public string MyFunction(string myParameter)
     {
            // Here I need to be able to detect the code language from the incoming request, and somehow to be able to translate 'Couleur' from the specified language...
            return myParameter + "_Couleur";
     }
 }

我有以下 WCF 客户端,它像这样调用 WCF 服务:

    static void Main(string[] args)
    {
        ServiceReference.MyService myService = new ServiceReference.MyService();

        // Calling the method MyFunction with parameter 'AAA'
        string result = myService.MyFunction("AAA"); // result = 'AAA_Couleur'
    }
        

而且我不知道如何在 WCF 中实现这一点,以及如何从客户端传递包含语言代码的 header?

我尝试从客户端使用 var msgHeader = MessageHeader.CreateHeader("语言", "", "EN", false); OperationContext.Current.OutgoingMessageHeaders.Add(msgHeader);

并从 WCF 服务中检索代码,但没有成功。

如有任何想法,我们将不胜感激。 非常感谢

你可以试试Message inspector,在客户端,通过实现IClientMessageInspector接口来拦截SOAP消息。在服务器端,通过实现IDispatchMessageInspector接口来拦截SOAP消息。

这是服务器端的示例:

 public class CustomMessageInspector : IDispatchMessageInspector
     {
         public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
         {
             MessageHeader header = MessageHeader.CreateHeader("UserAgent", "http://User", "User1");
             request.Headers.Add(header);
             return null;
         }
    
         public void BeforeSendReply(ref Message reply, object correlationState)
         {
             MessageHeader header1 = MessageHeader.CreateHeader("Testreply", "http://Test", "Test");
             reply.Headers.Add(header1);
         }
     }
     [AttributeUsage(AttributeTargets.Interface)]
     public class CustomBehavior : Attribute, IContractBehavior
     {
         public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
         {
             return;
         }
    
         public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
         {
             return;
         }
         public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
         {
             dispatchRuntime.MessageInspectors.Add(new CustomMessageInspector());
         }
    
         public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
         {
             return;
         }
     }

这是客户端的示例:

         public class ClientMessageLogger : IClientMessageInspector
 {
     public object AfterReceiveRequest(ref Message reply, object correlationState)
     {
         MessageHeader header = MessageHeader.CreateHeader("UserAgent", "http://User", "User1");
         reply.Headers.Add(header);
         return null;
     }
    
     public void BeforeSendRequest(ref Message request, IClientChannel channel)
     {
         MessageHeader header1 = MessageHeader.CreateHeader("Testreply", "http://Test", "Test");
         request.Headers.Add(header1);
     }
 }
 [AttributeUsage(AttributeTargets.Interface)]
 public class CustomBehavior : Attribute, IContractBehavior
 {
     public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
     {
         return;
     }
    
     public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
     {
         return;
     }
     public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
     {
         dispatchRuntime.MessageInspectors.Add(new CustomMessageInspector());
     }
    
     public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
     {
         return;
     }
 }

在服务接口上方添加 Custombehavior 以应用消息检查器。

   [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
         [CustomBehavior]
      public interface IDemo