我有一個WCF服務,我需要接收一個語言代碼(EN代表英語,或者DE代表德語,或者IT代表意大利語),以使我的WCF服務的所有方法提供的結果國際化。下面是我的WCF服務和方法的簡化版本,以及我使用的客戶端。我需要根據從請求頭中獲取的語言代碼來翻譯該方法的結果。
//IMyService
[ServiceContract, XmlSerializerFormat] 。
public interface IMyServiceOperationContract]。
string MyFunction(string myParameter) ;
}
//MyService; }
public class MyService : IMyService
{
public string MyFunction(string myParameter)?
{
//這里我需要能夠從傳入的請求中檢測出代碼語言,并以某種方式能夠從指定的語言中翻譯出'Couleur'...
return myParameter "_Couleur"/span>。
}
而我有以下WCF客戶端,它像這樣呼叫WCF服務:
static void Main(string[] args)
{
ServiceReference.MyService myService = new ServiceReference.MyService()。
//呼叫帶有引數'AAA'的MyFunction方法。
string result = myService.MyFunction("AAA"); // result = 'AAA_Couleur'。
}
我不知道如何在WCF中實作這一點,以及如何從客戶端傳遞一個包含語言代碼的頭?
我試圖從客戶端使用 var msgHeader = MessageHeader.CreateHeader("Language", "", "EN", false)。 OperationContext.Current.OutgoingMessageHeaders.Add(msgHeader);
并從WCF服務中檢索代碼,但沒有成功。
如果有任何想法,我們將非常感激。 非常感謝
uj5u.com熱心網友回復:
你可以嘗試訊息檢查器,在客戶端,通過實作IClientMessageInspector介面來攔截SOAP訊息。在服務器端,通過實作IDispatchMessageInspector介面來攔截SOAP訊息。
這是在服務器端的例子:
這是在服務器端的例子。
public class CustomMessageInspector : IDispatchMessageInspectorpublic object AfterReceiveRequest(>ref Message request, IClientChannel channel, InstanceContext instanceContext)。
{
MessageHeader header = MessageHeader.CreateHeader("UserAgent"/span>, "http://User"/span>, "User1"/span>);
request.Headers.Add(header)。
return null。
}
public void BeforeSendReply(ref Message reply。object correlationState)。
{
MessageHeader header1 = MessageHeader.CreateHeader("Testreply"/span>, "http://Test"/span>, "Test"/span>);
reply.Headers.Add(header1)。
}
}
[AttributeUsage(AttributeTargets.Interface)]
public class CustomBehavior : Attribute,IContractBehavior
{
public void AddBindingParameters(ContractDescription合同描述。服務端點endpoint, BindingParameterCollection bindingParameters)。
{
return。
}
public void ApplyClientBehavior(ContractDescription合同描述。ServiceEndpoint endpoint, ClientRuntime clientRuntime)。
{
return。
}
public void ApplyDispatchBehavior(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"/span>, "http://User"/span>, "User1"/span>);
reply.Headers.Add(header)。
return null。
}
public void BeforeSendRequest(ref Message request, IClientChannel channel)。
{
MessageHeader header1 = MessageHeader.CreateHeader("Testreply"/span>, "http://Test"/span>, "Test"/span>);
request.Headers.Add(header1)。
}
}
[AttributeUsage(AttributeTargets.Interface)]
public class CustomBehavior : Attribute,IContractBehavior
{
public void AddBindingParameters(ContractDescription合同描述。服務端點endpoint, BindingParameterCollection bindingParameters)。
{
return。
}
public void ApplyClientBehavior(ContractDescription合同描述。ServiceEndpoint endpoint, ClientRuntime clientRuntime)。
{
return。
}
public void ApplyDispatchBehavior(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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/316171.html
標籤:
