views:

1270

answers:

8

I have a windows forms client that consumes an ASP.net web service.

It's my understanding that when an exception is thrown by the web service it will be converted to a SoapException in the .net client software.

Well, I am catching the SoapException, but when I display the message on the client end in a messagebox, it shows more information than I really want to show.

Here is the webservice code:

throw new ApplicationException("Invalid username or password.");

Or even:

throw new SoapException("Invalid username or password.", SoapException.ServerFaultCode);

But on the client end, the SoapException's Message property is this:

System.Web.Services.Protocols.SoapException: Invalid username or password. at Transportation.InsertSignUps(String username, String password, SignUp[] signUps) in c:\Documents and Settings\rdoverby\My Documents\Visual Studio 2008\WebSites\www.gtcc.edu\App_Code\webservices\Transportation.cs:line 50

Whereas, all I really want to show is:

Invalid username or password.

How can I extract ONLY the original exception's message from the SoapException. I have poked around the SoapException object in the VS debugger, but I can't find what I am looking for.

Thanks.

EDIT

Of course, I don't want to parse that long string.

A: 

Tried Exception.InnerException.Message ?

Cerebrus
Yes. InnerException is null. There is no way to get to the original exception from client code.
Ronnie Overby
A: 

Derive a new exception class from SoapException and set the message property directly.

Brad_Z
I can't do that. It doesn't matter whether I throw an Exception, ApplicationException, SoapException, CustomSoapException, or a YourMomTurnsTricksException. The webservice will send xml <Fault> and the consumer will turn that into a SoapException.
Ronnie Overby
So then the extra text is added on the client side? Interesting. I guess you could manipulate the serialization of the exception but that sounds hard. Probably just parse the string :p
Brad_Z
A: 

You need to throw your own exception, custom or whatever, and then when the client throws the soap exception, the Detail property should have what you're looking for.

Joshua Belden
A: 

Screw it. I just returned a string from the service containing any error messages. If the string is null then the service did its job.

Silence is golden.

Ronnie Overby
A: 

Don't be too disappointed:)

I've did the same with the help of this article: http://www.developer.com/net/csharp/article.php/3088231.

I've created one custom class for my exceptions, and it even has the real Exception as an InnerException encapsulated in it. (I've added a language-independent ID to every known type of my custom Exceptions, to make it more usable for my clients.)

On the client side, I just get the InnerException, and InnerException.Message is the original text, without any garbage.

balint
A: 

Sorry I didn't see this before.

I believe the true answer is to configure <customErrors/> on the server. I forget which mode you want, but one of the three will limit the amount of detail in the exception that the client receives. It's not what you'd expect, and I didn't believe it myself. Someone posted this answer on another forum and when I tried it, it worked. Nothing I had tried got rid of the detail.

John Saunders
A: 

public static string GetSoapExceptionDetail(SoapException SoapEx, string CallingMethodName) { string errMsg = "Error! ";

        const string wb = "#whitespace";

        errMsg = string.Concat(errMsg, CallingMethodName, " SoapException: ", "<br />");

        XmlNode xmlNode = SoapEx.Detail;

        foreach (XmlNode childNode in xmlNode.ChildNodes)
        {
            if (childNode.Name.ToLower().EndsWith("fault"))
            {
                errMsg = string.Concat(errMsg, "<br />", "<ul>");

                foreach (XmlNode node in childNode.ChildNodes)
                {
                    if (node.Name.ToLower().EndsWith("code"))
                    {
                        errMsg = string.Concat(errMsg, "<li>", "Error Code: ", node.InnerText, "</li>");
                    }
                    else if (node.Name.ToLower().EndsWith("message"))
                    {
                        errMsg = string.Concat(errMsg, "<li>", "Error Message: ", node.InnerText, "</li>");
                    }
                    else if (node.Name.ToLower().EndsWith("errors"))
                    {
                        errMsg = string.Concat(errMsg, "<li>", "Errors: ", "<br />", "<ul>");

                        foreach (XmlNode xNode in node.ChildNodes)
                        {
                            if (xNode.Name.ToLower().EndsWith("index"))
                            {
                                errMsg = string.Concat(errMsg, "<li>", "Index: ", xNode.InnerText, "</li>");
                            }
                            else if (xNode.Name.ToLower().EndsWith("field"))
                            {
                                errMsg = string.Concat(errMsg, "<li>", "Field: ", xNode.InnerText, "</li>");
                            }
                            else if (xNode.Name.ToLower().EndsWith("trigger"))
                            {
                                errMsg = string.Concat(errMsg, "<li>", "Trigger: ", xNode.InnerText, "</li>");
                            }
                            else if (xNode.Name.ToLower().EndsWith("code"))
                            {
                                errMsg = string.Concat(errMsg, "<li>", "Code: ", xNode.InnerText, "</li>");
                            }
                            else if (xNode.Name.ToLower().EndsWith("isexemptable"))
                            {
                                errMsg = string.Concat(errMsg, "<li>", "Is Exemptable: ", xNode.InnerText, "</li>");
                            }
                            else if (xNode.Name.ToLower().EndsWith("detail"))
                            {
                                errMsg = string.Concat(errMsg, "<li>", "Detail: ", xNode.InnerText, "</li>");
                            }
                            else if (!(xNode.Name.ToLower().EndsWith(wb)))
                            {
                                errMsg = string.Concat(errMsg, "<li>", xNode.InnerText, "</li>");
                            }
                        }

                        errMsg = string.Concat(errMsg, "</ul>", "</li>");
                    }
                    else if (!(node.Name.ToLower().EndsWith(wb)))
                    {
                        errMsg = string.Concat(errMsg, "<li>", node.InnerText, "</li>");
                    }
                }

                errMsg = string.Concat(errMsg, "</ul>");
            }
            else if (!(childNode.Name.ToLower().EndsWith(wb)))
            {
                errMsg = string.Concat(errMsg, "<br />", childNode.InnerText);
            }
        }

        errMsg = string.Concat(errMsg, "</ul>");

        return errMsg;
    }

try this method

Senthil S R
-1: That looks absolutely horrible.
Alex Angas