1

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.

Dennis Lukas
  • 483
  • 2
  • 6
  • 20
  • It goes without saying that I've tried my best to hide irrelevant code, but if I have for some reason omitted something you'd need in order to help me, please tell me. – Dennis Lukas Dec 18 '18 at 11:12
  • You would use a "Current"-object and store the correlationState (or whatever you want to have available during the logical request) in there. See [this answer](https://stackoverflow.com/a/1895958/21567) for an example. – Christian.K Dec 19 '18 at 12:05

2 Answers2

1

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;
    }
}
Ackelry Xu
  • 756
  • 3
  • 6
  • Thanks for your answer. I have already solved my issue by adding the GUID as a header to the incoming message because i was already using OperationContext anyway, but your solution seems better therefore I have marked your response as the answer. – Dennis Lukas Jan 07 '19 at 09:58
0

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

Popo
  • 2,402
  • 5
  • 33
  • 55