tags:

views:

245

answers:

2

For logging purposes, I want to get at the raw request sent to my RESTful web service implemented in WCF.

I have already implemented IDispatchMessageInspector. In my implementation of AfterReceiveRequest, I want to spit out the raw bytes of the message even (and especially) if the content of the message is invalid. This is for debugging purposes. My service works perfectly already, but it is often helpful when working through problems with clients who are trying to call the service to know what it was they sent, i.e., the raw bytes.

For example, let's say that instead of sending a well-formed XML document, they post the string "your mama" to my service endpoint. I want to see that that's what they did. Unfortunately using MessageBuffer::CreateBufferedCopy() won't work unless the contents of the message are already well-formed XML.

Here's (roughly) what I already have in my implementation of AfterReceiveRequest:

// The immediately following line raises an exception if the message
// does not contain valid XML. This is uncool because I want
// the raw bytes regardless of whether they are valid or not.
using (MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue))
{
    using (MemoryStream stream = new MemoryStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        buffer.WriteMessage(stream);
        stream.Position = 0;
        Trace.TraceInformation(reader.ReadToEnd());
    }
    request = buffer.CreateMessage();
}

My guess here is that I need to get at the raw request before it becomes a Message. This will most likely have to be done at a lower level in the WCF stack than an IDispatchMessageInspector.

Anyone know how to do this?

+1  A: 

You want to author an encoder; this pulls the bytes off the wire to create a message (before all the protocol channels start validating it and whatnot).

http://msdn.microsoft.com/en-us/library/ms751486.aspx

Brian
Thanks a google! That did the trick.
Gregory Higley
A: 

If all you want to do is to log the messages, and not process them in any way, then just enable the logging as detailed here.

http://msdn.microsoft.com/en-us/library/ms730064.aspx

Mark