Web Service Exception Handling
- by SchlaWiener
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?