I have a WCF service, and I need to receive a language code (EN for English, or DE for Deutsch, or IT for Italian) to internationalize the result provided by all method of my WCF service. Below is a simplified version of my WCF service and method, and the client I use. And I need to translate the result of the method depending on a language code I retrieve from a header of the request.
//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";
}
}
And I have the following WCF Client, which calls the WCF service like that:
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'
}
And I do not know how to achieve this in WCF, and how to pass a header containing the language code from the client?
I tried to use from the client var msgHeader = MessageHeader.CreateHeader("Language", "", "EN", false); OperationContext.Current.OutgoingMessageHeaders.Add(msgHeader);
And to retrieve the code from the WCF service but with no luck.
Any ideas would be greatly appreaciated. Thank you very much
CodePudding user response:
You can try Message inspector, On the client side, by implementing the IClientMessageInspector interface to intercept SOAP messages. On the server side, by implementing the IDispatchMessageInspector interface to intercept SOAP messages.
This is the sample on server side:
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;
}
}
This the sample on the client side:
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;
}
}
Add Custombehavior above service interface to apply the message inspector.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
[CustomBehavior]
public interface IDemo