Logging WCF Requests and Responses using Message Inspectors on an MSMQ Endpoint

2.2k Views Asked by At

I'm working on a WCF service layer, which utilizes message inspectors for logging request and reply soap messages.

Here's the inspector, stripped of irrelevant code;

public class ServiceInspectorBehavior : Attribute, IDispatchMessageInspector, IServiceBehavior
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        // pass request to BeforeSendReply method
        return request.ToString();
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        string requestMessage = (string)correlationState;
        string responseMessage = reply.ToString();
        LogManager.Log(request, response);
    }
}

It was all fine and dandy, until a new business requirement came along, which will work best with MSMQ, so I'm in the process of implementing an MSMQ endpoint. The problem here is that the messaging is one-way.

[OperationContract(Name = "EnqueueRequest", IsOneWay = true)]

Even if I build a response object, the 'reply' will be null in the inspector's BeforeSendReply method, as none would be sent back to the client.

The only thing that seems plausible at the moment, is to move logging from message inspectors, into my message processor logic, where I handle the request, and build the response. I still prefer to keep my current setup if possible though.

The question is, although seemingly unlikely, "Is it possible to have a reply message in the inspector's BeforeSendReply method parameters, using MSMQ?".

Thanks in advance. Regards.


edit 2015.08.27

I've ditched logging using Message Inspector. Now logging in my service layer. Here's what I've done. I'm still open to suggestions.

public ResponseBase ProcessRequest(RequestBase requestObject)
{
    string requestString = string.Empty;
    string responseString = string.Empty;

    try
    {
        requestString = DataContractSerializerHelper.SerializeObject(requestObject);

        // ...
        // Do magic stuff, build a response object, etc
        // ...

        responseString = DataContractSerializerHelper.SerializeObject(responseObject);
        return responseObject;
    }
    catch(Exception e)
    {
        // Handle/log exception, return a default response, etc
    }
    finally
    {
        Logger.Log(requestString, responseString, ...);
    }
}
1

There are 1 best solutions below

0
On

Is it possible to have a reply message in the inspector's BeforeSendReply method parameters, using MSMQ?

No it is not.