Sunday, December 29, 2013

Soap Web Services Troubleshooting Tip

Intro

On many an occasion I've had something go wrong with a web service. Maybe a user calls in to report an error message being returned, or perhaps I've got my own exception handling that reports an exception was handled. For the sake of this article I'll assume you've got good exception handling and are logging exceptions that are thrown in your web service. Now let's pretend though that the stack trace by itself just isn't good enough. I've had one web service that threw a tricky error during a sql call, and the exception said it didn't like one of the parameter values. Well thanks, that helps a ton! Now if I can just figure out which of the 20 parameters it was...

Tha Meat Yo!

We can log the entire soap request! If this doesn't help debug the problem then my name isn't Jehosephat McJunkNStuff. Let's see how to log the full soap request:

    /// 
    /// Retrieves the full xml of the raw soap request. We use this for logging purposes.
    /// 
    /// the full xml of the raw soap request
    private string GetRawSoapRequest()
    {
      string soapRequest = "";
      try
      {
        // Get raw request body
        if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.InputStream != null)
        {
          using (Stream receiveStream = HttpContext.Current.Request.InputStream)
          {
            // Move to begining of input stream and read
            receiveStream.Position = 0;
            using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
              soapRequest = readStream.ReadToEnd();
          }
        }
      }
      catch { /*eat it*/ }
      return soapRequest;
    }

Copy the above method into your web service. As you can see, what it does is get the full raw SOAP request as a stream and reads it into a string value, which it then returns. Because it's a soap request this value is XML. As for what you do with this value, well that's entirely up to you! Here's a quick sample containing some not-fully-fleshed-out code that logs the raw soap of the request in an exception handler:

try
{
    //...
}
catch (Exception e)
{
    DoLogException(e, GetRawSoapRequest());
}

Armed with the raw SOAP request XML, you can now set about debugging the exception by recreating the exact request to your web service, stepping through it in the debugger to see where the exception lies. Heck, you might not even have to; I've seen some requests where there was one obviously bad value so use of the debugger wasn't even required. That's it for this week's blog, stay tuned for next week! I suppose that phrase "stay tuned" really doesn't make sense in the context of teh interwebs. Heck it really wouldn't make sense in much of any context for a full week...who would "stay tuned" to a single tv station or radio station for an entire week? Well, have a good week anyways, hope you enjoy the blog.

No comments:

Post a Comment