How do I extract the inner exception from a soap exception in ASP.NET? How do I extract the inner exception from a soap exception in ASP.NET? asp.net asp.net

How do I extract the inner exception from a soap exception in ASP.NET?


Unfortunately I don't think this is possible.

The exception you are raising in your web service code is being encoded into a Soap Fault, which then being passed as a string back to your client code.

What you are seeing in the SoapException message is simply the text from the Soap fault, which is not being converted back to an exception, but merely stored as text.

If you want to return useful information in error conditions then I recommend returning a custom class from your web service which can have an "Error" property which contains your information.

[WebMethod]public ResponseClass HelloWorld(){  ResponseClass c = new ResponseClass();  try   {    throw new Exception("Exception Text");    // The following would be returned on a success    c.WasError = false;    c.ReturnValue = "Hello World";  }  catch(Exception e)  {    c.WasError = true;    c.ErrorMessage = e.Message;    return c;  }}


It IS possible!

Service operation example:

try{   // do something good for humanity}catch (Exception e){   throw new SoapException(e.InnerException.Message,                           SoapException.ServerFaultCode);}

Client consuming the service:

try{   // save humanity}catch (Exception e){   Console.WriteLine(e.Message);    }

Just one thing - you need to set customErrors mode='RemoteOnly' or 'On' in your web.config (of the service project).

credits about the customErrors discovery - http://forums.asp.net/t/236665.aspx/1


I ran into something similar a bit ago and blogged about it. I'm not certain if it is precisely applicable, but might be. The code is simple enough once you realize that you have to go through a MessageFault object. In my case, I knew that the detail contained a GUID I could use to re-query the SOAP service for details. The code looks like this:

catch (FaultException soapEx){    MessageFault mf = soapEx.CreateMessageFault();    if (mf.HasDetail)    {        XmlDictionaryReader reader = mf.GetReaderAtDetailContents();        Guid g = reader.ReadContentAsGuid();    }}