Handling Web Services Exceptions

I have a Winforms application that uses C # Webservice. If the WebService throws an application "Exception", my application always gets a SoapException instead of a "real" exception.

Here's a demo:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {
        throw new IndexOutOfRangeException("just a demo exception");
    }
}

      

Now, on the client side, I want to be able to handle different exceptions in a different way.

        try
        {
            ServiceReference1.Service1SoapClient client
                = new ServiceReference1.Service1SoapClient();
            Button1.Text = client.HelloWorld();
        }
        catch (IndexOutOfRangeException ex)
        {
            // I know how to handle IndexOutOfRangeException
            // but this block is never reached
        }
        catch (MyOwnException ex)
        {
            // I know how to handle MyOwnException
            // but this block is never reached
        }
        catch (System.ServiceModel.FaultException ex)
        {
            // I always end in this block
        }

      

But this doesn't work because I always get "System.ServiceModel.FaultException" and I can only figure out the "real" exception by parsing the message property "Exception":

        System.Web.Services.Protocols.SoapException: Server was unable
        to process request. ---> System.IndexOutOfRangeException: just a demo\n
           at SoapExceptionTest.Service1.Service1.HelloWorld() in ...
        --- End of inner exception stack trace ---

      

Is there a way to make this work somehow?

+2


a source to share


2 answers


In my experience, web services will return exceptions serialized in the response. The client then needs to deserialize them and take appropriate action.



A quick google search turned around: http://msdn.microsoft.com/en-us/library/ds492xtk%28vs.71%29.aspx

+1


a source


Remember that SOAP is not aware of .NET exceptions, errors in web services are returned as "Errors". You should consider designing your web services so that they either catch errors or return error information as part of the response and your client code handles that. Or yu can work with SOAP fault mechanism.

This old but helpful MSDN article can help you



http://msdn.microsoft.com/en-us/library/aa480514.aspx

+1


a source







All Articles