Question

How could a string be passed with every call of a WCF service (without singleton). For instance, I need the CultureInfo string to be passed with every call. Earlier in Remoting I useed to pass the data from client like

CallContext.LogicalSetData("CultureInfo", "en-Us")

and retrieve in server (running in different appdomain) like

CallContext.LogicalGetData("CultureInfo").

This is not working in WCF. Always the CallContext.LogicalGetData is null.

Was it helpful?

Solution

Unfortunately in WCF, it isn't quite as easy. However, you can do it once for all operations. You can implement IClientMessageInspector which allows you to modify every request before sending it. Then add your custom message header. Note that this is a Soap Message Header not an HTTP header.

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request,
    System.ServiceModel.IClientChannel channel)
{
    request.Headers.Add(MessageHeader.CreateHeader("CultureInfo",
        "http://mynamespace", Thread.CurrentThread.CurrentUICulture.Name));
    return null;
}

On the server side, implement IDispatchMessageInspector

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request,
    IClientChannel channel, InstanceContext instanceContext)
{
    string culture = request.Headers.GetHeader<string>("CultureInfo",
        "http://mynamespace");
    Thread.CurrentThread.CurrentCulture
        = CultureInfo.CreateSpecificCulture(culture);
    return null;
}

You'll also need to implement some custom behaviors to apply the message inspectors. For implementing the behaviors, see the MSDN pages:

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector%28v=vs.110%29.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top