Reputation: 938
I throw a few soap exceptions in my web service successfully. I would like to catch the exceptions and access the string and ClientFaultCode that are called with the exception. Here is an example of one of my exceptions in the web service:
throw new SoapException("You lose the game.", SoapException.ClientFaultCode);
In my client, I try to run the method from the web service that may throw an exception, and I catch it. The problem is that my catch blocks don't do anything. See this example:
try
{
service.StartGame();
}
catch
{
// missing code goes here
}
How can I access the string and ClientFaultCode that are called with the thrown exception?
Upvotes: 13
Views: 34561
Reputation: 428
If you want to get the full SOAP response and get an error message with more detail you can start with this:
catch(Exception ex)
{
string inner_message = string.Empty;
try
{
FaultException faultException = (FaultException)ex;
MessageFault msgFault = faultException.CreateMessageFault();
XmlElement elm = msgFault.GetDetail<XmlElement>();
inner_message = elm.InnerText;
}
string full_message = ex.Message + " " + inner_message;
}
Based on the complexity of the XML you can try and get inner tags or info on the way you need. The XmlElement variable will store all the info.
Upvotes: 0
Reputation: 26668
You may want to catch the specific exceptions.
try
{
service.StartGame();
}
catch(SoapHeaderException)
{
// soap fault in the header e.g. auth failed
}
catch(SoapException x)
{
// general soap fault and details in x.Message
}
catch(WebException)
{
// e.g. internet is down
}
catch(Exception)
{
// handles everything else
}
Upvotes: 10
Reputation: 69392
Catch the SoapException
instance. That way you can access its information:
try {
service.StartGame();
} catch (SoapException e) {
// The variable 'e' can access the exception's information.
}
Upvotes: 8