Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncing correlationstate between IClientMessageInspector and IParameterInspector

Tags:

wcf

I have a wcf client. According to requirements, I need to record some of the metadata in the request (as well as user data which is not included in the request.) Then, if the request is successful I may have to record response metadata and depending on flags, the full soap request.

I am trying to do this the right way (using IParameterInspector to examine the metadata and IClientMessageInspector to get the Soap), but I have no way of correlating the two Interface requests. I am not sure about thread safety here. This is a stripped down version of where I am at...

     public class SoapRequestInfo
{
    public string UserId { get; set; }
    public Guid Key { get; set; }
    //would contain a lot more info
}

public class OperationProfilerParameterInspector : IParameterInspector, IClientMessageInspector
{
    //before serialization
    public object BeforeCall(string operationName, object[] inputs) //IParameterInspector
    {
        //Add the operation, record some specific inputs to db
        return new SoapRequestInfo
                            {
                                UserId = "1234",
                                Key = new Guid()
                            };
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) //IParameterInspector
    {
       var info = correlationState as SoapRequestInfo;
        //Do some additional logging - easy enough
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel) //IClientMessageInspector
    {
        //want to correlate this with IParameterInspector
        return null;
    }


    public void AfterReceiveReply(ref Message reply, object correlationState) //IClientMessageInspector
    {
        //May want to log full soap message depending on after call criteria
    }
}      

I know I can't use a private variable to hold the Guid. I can't use session, because there can be multiple requests in close succession and can't guarantee the response is correct. So how can I uniquely identify the correlationState between the two interfaces?

like image 954
Frank M Avatar asked Dec 28 '12 17:12

Frank M


1 Answers

You can probably use HttpContext.Items to keep your object if your service is running in ASPNET compatibility mode otherwise you can use TLS (Thread Local Storage), put the data in a slot and fetch/clear later.

like image 156
asolvent Avatar answered Sep 22 '22 02:09

asolvent