How can I get at the raw bytes of the request in WCF?
- by Gregory Higley
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?