IDispatchMessageInspector CorrelationState, and accessing it in between

584 Views Asked by At

My AfterRecieveRequest method generates a GUID, and passes this on through the correlationState variable to the BeforeSendReply method.

Between those two calls, a lot happens in my WCF service, and I'd like to access this GUID from within the Webservice methods. Is there a way for me to access this object throughout the WCF service?

The GUID is used for logging purposes, as I am calling on the API of a different application and want to log the results, and log them under the GUID generated in the IDispatchMessageInspector implementation.

for example:

IDispatchMessageInspector implementation:

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
    var correlationState = GUID.NewGuid();
    StaticLogger.AddLog(originalMessage, correlationState.ToString(), "WCF-Incoming");
    return correlationState;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
    StaticLogger.AddLog(originalMessage, correlationState.ToString(), "WCF-Outgoing");

}

WCF Service:

public ResponseObject WCFMethod(string param1, string param2)
{
    ResponseObject response = new ResponseObject();

    Client client = new Client();
    ClientObject result = client.OutsideOperation(param1,param2);
    // here, i would like to log the result object and use the GUID from the correlationstate
    StaticLogger.AddLog(result, correlationState.ToString(), WCF-Internal )

    response.resultAttribute = result;

    return response;
}

How would I go on to accomplish this? I've been thinking about using a ThreadStatic attribute so the thread keeps the GUID somewhere in memory, but I am afraid my understanding of this subject is insufficient to implement it right now.

2

There are 2 best solutions below

1
On BEST ANSWER

If you only want to access the guid between BeforeSendReply and AfterReceiveRequest, you could use MessageProperties, the MessageProperties could be accessed when you execute your service.

Below is my test.

  public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        var correlationState = Guid.NewGuid();
        request.Properties["myGuid"] = correlationState; // store guid in Properties
        return correlationState;

    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {

    }

Then you could get the guid in your service.

 public class ServiceTest : Test
{

    public double add(double a, double b)
    {
         // get the property through IncomingMessageProperties property
        Guid gu = (Guid)OperationContext.Current.IncomingMessageProperties["myGuid"];
        Console.WriteLine(gu);

        return a + b;
    }
}
0
On

Not sure this would be what you want, but you can probably add the GUID to your channel message header. Someone already wrote up some useful information on this: https://stackoverflow.com/a/1408177/2016162