<p>I have a simple web service operation like this one:</p>

<pre><code>    [WebMethod]
    public string HelloWorld()
    {
        throw new Exception("HelloWorldException");
        return "Hello World";
    }
</code></pre>

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

<pre><code>    try
    {
        hwservicens.Service1 service1 = new hwservicens.Service1();
        service1.HelloWorld();
    }
    catch(Exception e)
    {
        Console.WriteLine(e.ToString());
    }
</code></pre>

<p>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 <code>SoapException</code>, which is fine, but it's <code>Message</code> property is like this...</p>

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

<p>...and the <code>InnerException</code> is <code>null</code>.</p>

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

[{'answer_id': 32508, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 4, 'selected': True, 'text': '<p>Unfortunately I don\'t think this is possible.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<pre><code>[WebMethod]\npublic ResponseClass HelloWorld()\n{\n  ResponseClass c = new ResponseClass();\n  try \n  {\n    throw new Exception("Exception Text");\n    // The following would be returned on a success\n    c.WasError = false;\n    c.ReturnValue = "Hello World";\n  }\n  catch(Exception e)\n  {\n    c.WasError = true;\n    c.ErrorMessage = e.Message;\n    return c;\n  }\n}\n</code></pre>\n'}, {'answer_id': 33525, 'author': 'Jacob Proffitt', 'author_id': 1336, 'author_profile': 'https://Stackoverflow.com/users/1336', 'pm_score': 2, 'selected': False, 'text': '<p>I ran into something similar a bit ago and <a href="http://theruntime.com/blogs/jacob/archive/2008/01/28/getting-at-the-details.aspx" rel="nofollow noreferrer">blogged about it</a>. 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:</p>\n\n<pre><code>catch (FaultException soapEx)\n{\n    MessageFault mf = soapEx.CreateMessageFault();\n    if (mf.HasDetail)\n    {\n        XmlDictionaryReader reader = mf.GetReaderAtDetailContents();\n        Guid g = reader.ReadContentAsGuid();\n    }\n}\n</code></pre>\n'}, {'answer_id': 12281546, 'author': 'BornToCode', 'author_id': 1057791, 'author_profile': 'https://Stackoverflow.com/users/1057791', 'pm_score': 3, 'selected': False, 'text': '<p>It <em>IS</em> possible!</p>\n\n<p>Service operation example:</p>\n\n<pre><code>try\n{\n   // do something good for humanity\n}\ncatch (Exception e)\n{\n   throw new SoapException(e.InnerException.Message,\n                           SoapException.ServerFaultCode);\n}\n</code></pre>\n\n<p>Client consuming the service:</p>\n\n<pre><code>try\n{\n   // save humanity\n}\ncatch (Exception e)\n{\n   Console.WriteLine(e.Message);    \n}\n</code></pre>\n\n<p><strong>Just one thing - you need to set customErrors mode=\'RemoteOnly\' or \'On\' in your web.config</strong> (of the service project).</p>\n\n<p>credits about the customErrors discovery - <a href="http://forums.asp.net/t/236665.aspx/1" rel="nofollow">http://forums.asp.net/t/236665.aspx/1</a></p>\n'}]
['https://Stackoverflow.com/questions/32058', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3379/']