Question

I have a simple web service operation like this one:

    [WebMethod]
    public string HelloWorld()
    {
        throw new Exception("HelloWorldException");
        return "Hello World";
    }

And then I have a client application that consumes the web service and then calls the operation. Obviously it will throw an exception :-)

    try
    {
        hwservicens.Service1 service1 = new hwservicens.Service1();
        service1.HelloWorld();
    }
    catch(Exception e)
    {
        Console.WriteLine(e.ToString());
    }

In my catch-block, what I would like to do is extract the Message of the actual exception to use it in my code. The exception caught is a SoapException, which is fine, but it's Message property is like this...

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: HelloWorldException
   at WebService1.Service1.HelloWorld() in C:\svnroot\Vordur\WebService1\Service1.asmx.cs:line 27
   --- End of inner exception stack trace ---

...and the InnerException is null.

What I would like to do is extract the Message property of the InnerException (the HelloWorldException text in my sample), can anyone help with that? If you can avoid it, please don't suggest parsing the Message property of the SoapException.

Was it helpful?

Solution

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;
  }
}

OTHER TIPS

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top