Take Control of Web Service Exceptions

0 comments suggest edit

Craig Andera posts a technique for handling exceptions thrown by a webservice. He takes the approach of adding a try catch block to each method.

A while ago I tackled the same problem, but I was unhappy with the idea of wrapping every inner method call with a try catch block. I figured there had to be a better way. Since SOAP is simply XML being sent over a wire, I figured there had to be a way for me to hook into the pipeline rather than modify my code.

What I came up with is my Exception Injection Technique Using a Custom Soap Extension. This allows you to simply add an additional attribute to each web method as in the sample below and have full control over how exceptions are handled and sent over the wire.

[WebMethod, SerializedExceptionExtension]

public string ThrowNormalException()

{

    throw new ArgumentNullException(“MyParameter”, \         ”Exception thrown for testing purposes”);

}

Read about the technique here and feel free to adapt it to your purposes.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

4 responses

  1. Avatar for Jeff Atwood
    Jeff Atwood November 2nd, 2005

    Yes, I reached the same conclusion:



    Throwing Better .NET Exceptions with SOAP and HTTP

    http://www.codinghorror.com/blog/archives/000108.html

  2. Avatar for Haacked
    Haacked November 2nd, 2005

    Nice! I didn't see your article when I was researching my solution.

  3. Avatar for Maths
    Maths March 26th, 2007

    This seem to be the best approach. Now, the infrastructure code can take care of managing exception. I am already planning to use this in my current project.

  4. Avatar for Maths
    Maths March 26th, 2007

    This seem to be the best approach. Now, the infrastructure code can take care of managing exception. I am already planning to use this in my current project.