Question

I have a WCF Service hosted in IIS/ASP.NET that accepts HTTP Post (not form post) of serialized objects.

If the client sends malformed requests (eg they're not serializing the object correctly) I'd like to log the message sent up.

We're already using ELMAH to capture unhandled exceptions, so simply attaching the post data would be the easiest option.

I can get the current HttpContext during an exception, however this does only contains the HTTP Header information.

My question is this: Is there some way of capturing the original HTTP POST request body? Or, failing that - a better way (without a reverse proxy) of capturing the input that caused the error?

Edit: Just to clarify, running packet-level capturing at all times isn't really suitable. I'm after a solution that I can deploy to Production servers, and which will have clients outside our control or ability to monitor.

Edit #2: A suggestion was made to access the Request.InputStream - this doesn't work if you're trying to read after WCF has read the request off the stream.

A sample piece of code to see how I've tried using this is here.

        StringBuilder log = new StringBuilder();

        var request = HttpContext.Current.Request;

        if (request.InputStream != null)
        {
            log.AppendLine(string.Format("request.InputStream.Position = \"{0}\"", request.InputStream.Position));
            if (request.InputStream.Position != 0)
            {
                request.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
            }

            using (StreamReader sr = new StreamReader(request.InputStream))
            {
                log.AppendLine(string.Format("Original Input: \"{0}\"", sr.ReadToEnd()));
            }
        }
        else
        {
            log.AppendLine("request.Inputstream = null");
        }


        log.ToString();

The ouput of log.ToString() is:

    request.InputStream.Position = "0"
    Original Input: ""
Was it helpful?

Solution

By the time it gets to your service the request is processed and not available to you.

However ... you could attach a message inspector. Message Inspectors allow you to fiddle with the message before it reaches your operation implementations. You could create a buffered copy of the message, and copy it into the OperationContext.Current.

Ugly hack of course, and it will mean memory overhead as now two copies of the message are floating about for every request.

OTHER TIPS

Did you look at the System.Web.Request.InputStream Property? It should have exactly what you want.

How to "rewind" the InputStream Property.

    if (Request.InputStream.Position != 0)
    {
        Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
    }

Another option you should look into is capturing this information with an HTTPModule on the BeginRequest event. The data should be there at BeginRequest event because I do not believe WCF picks up the request until after PostAuthenticateEvent.

from under ASP.NET (ASP web service under IIS) the following code helps:

if (request.InputStream.Position != 0)
{
  request.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
}

WCF maybe different (that is it Disposes InputStream after reading it)

Use fiddler. Free from MS. Works great.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top