views:

999

answers:

3

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: ""
A: 

Use fiddler. Free from MS. Works great.

Larry K
I'm talking about errors that have happened when there's no Fiddler, Wireshark, or whatever running to capture it.eg on a Production or Staging server where it's not feasible to run packet capturing all the time.
Will Hughes
A: 

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.

JD
I did look at that property - unfortunately that stream only contains unread data. By the time the exception has happened, the input data has already been read by WCF. You can't rewind/reset the position.
Will Hughes
See my update... Since Request.InputStream.CanSeek is true you can reset the position back to 0 by using the Seek method.
JD
Thanks for the update - CanSeek is true, however there's still nothing in the stream to be read. See updated text for example of what I've done to try and test this.
Will Hughes
+2  A: 

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.

blowdart
Thanks - it's not a particularly great solution, but sacrificing some memory will probably be worth it.
Will Hughes