views:

120

answers:

2

I have a Winforms app that consumes a C# Webservice. If the WebService throws an Exception my Client app always get's a SoapException instead of the "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 that does not work because I always get a "System.ServiceModel.FaultException" and I can only figure out the "real" exception by parsing the Exception's message property:

        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?

A: 

In my experience web services will return the exceptions serialized in the response. Then it's up to the client to deserialize them and take the appropriate action.

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

Seth Reno
A: 

You should keep in mind that SOAP does not know about .NET exceptions, errors in web services are returned as Faults. You should consider designing your web services so that they either catch the errors and return error information as part of the response, and your client code handles that. Or yu can work with the SOAP fault mechanism.

This old but useful MSDN article might help you

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

Chris Taylor