Apr 302012
 
Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

Here is the problem: How to properly display a user-friendly error message for an exception being thrown from an ASP.NET Web Service?

I know that we shouldn’t be really working anymore with ASP.NET Web Services as a huge amount of problems have been solved with the WCF framework. But, for those like me, that are working on some large-scale projects where we are still using some old technology this could be an interesting post.

I was kind of surprised the first time I saw an exception being thrown from the ASP.NET Web Service and how it looked ugly when the error Message Box appeared on the Client application (GUI). The Exception.Message contained the internal Web Service stack.

The server-side code looked like this:

[csharp]
[WebMethod]
public SomeResultObject SomeWebMethod()
{
try
{
/* code here */
}
catch (Exception ex)
{
/* Some logging logic here…*/
throw; //this would simply throw an SoapException
}
}
[/csharp]

The first thing I tried was to throw my Exception, and not just re-throwing, but this was not helping at all as I soon realized that all the exceptions thrown by the SomeWebMethod() would be wrapped internally by the .NET framework, and the SoapException would be thrown instead.

The first approach – Client side

My first approach of solving this problem was then to change the Client (GUI) application and display a generic error message, no matter what has been thrown by the Web Service. Obviously this was a bad choice, as there were so many conditions and error messages that the end-user needed to know about.

So, this approach would probably work for someone, but unfortunately didn’t work for me:

[csharp]
public void WebServiceCallMethod()
{
try
{
/* Calling the web service here */
}
catch(SoapException exc)
{
MessageBox.Show(“An error happened while processing your call”);
}
}
[/csharp]

Parsing the exc.Message in order to retrieve the error message was possible but was avoided and taken as a last resort. For sure this is not a correct approach as that is not really maintainable.

Fixing the Web Service – Return parameter

The second obvious solution was to change the Web Service, and the solution lies in the Rerturn object. I’ve soon realized that I could change my return parameter to include the ErrorMessage and to return that to the Client Application, as follows:

public class SomeResultObject
{
    public string ErrorMessage {get;set;}
    public bool ContainsErrors {get;set;}

    /* other object properties / methods */
}

[WebMethod]
public SomeResultObject SomeWebMethod()
{
    var returnObject = new SomeReturnObject();

    try
    {
        /* code here */
    }
    catch (Exception ex)
    {
        /* Some logging logic here...*/
       
        //we are not re-throwing an error, but setting 
        //the error message to the return object.
        returnObject.ContainsErrors = true;
        returnObject.ErrorMessage = ex.Message;
    }

    return returnObject;
}

This is so easy, even though is somehow polluting the return object with some infrastructure data.

Capturing the error message from the client application becomes as follows

public void WebServiceCallMethod()
{
    try
    {
        /* Calling the web service here */
        var returnObject = CallTheWebService();
        
        if(returnObject.ContainsErrors)
        {
                MessageBox.Show(returnObject.ErrorMessage);
        }
    }
    catch(SoapException exc)
    {
        //still capturing the error message from the server.
        MessageBox.Show(exc.Message);
    }
}

Final thoughts

While this is perhaps not the best approach, it solves the problem decently. Especially this is true when you have the possibility to change the server-side code in order to enable the client to properly display what sent.

    Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

    I'm a Software Developer and Solution Architect interested in Software Development, Object-Oriented Design and Software Architecture all this especially bound to the Microsoft.NET platform.Feel free to contact me or know more in the about section

      16 Responses to “ASP.NET Web Service – Returning properly formatted error message”

    1. Nice article, it also helped me.

    2. thank you.nice explain. saved my time.

    3. Thanks for this useful guide… waiting for more
      Thanks

    Leave a Reply

    weggepamula@mailxu.com walman