How to determine the type of exception thrown by asp.net web service?

I have an asp.NET web service (not WCF, but a classic .asmx with WebMethods) that throws exceptions. Exceptions are each of the two base classes of exceptions (both of which come from the exception):

public class InputException : Exception
{
   ....
}

public class FatalException : Exception
{
    ....
}

public class NoFilesFound: FatalException
{
   ....
}

....

The web service now throws an exception as needed. In my client code, I can catch the exceptions and see the message as follows:

Server was unable to process request. ---> There were no files found

FaultException ( , .GetType() catch). InputException FatalException ( , ). - , "--- > " . .

, SoapExceptions , , . , , XML, webservice XML, .

, , - , ?

+5
1

- SoapException. , , SoapException ( , , ), , - , SoapException XML XML node .

SoapException XML- Detail node, , .

WCF, FaultException<SomeFaultContract>.

. , , SoapException node:

[WebMethod]
public string HelloWorld()
{
    var doc = new XmlDocument();
    var node = doc.CreateNode(
        XmlNodeType.Element, 
        SoapException.DetailElementName.Name, 
        SoapException.DetailElementName.Namespace
    );
    // you could actually use any sub nodes here
    // and pass even complex objects
    node.InnerText = "no files found";

    throw new SoapException(
        "Fault occurred", 
        SoapException.ClientFaultCode, 
        Context.Request.Url.AbsoluteUri, 
        node
    );
}

SoapException :

using (var client = new WebService1())
{
    try
    {
        var result = client.HelloWorld();
    }
    catch (SoapException ex)
    {
        var detail = ex.Detail;
        // detail.InnerText will contain the detail message
        // as detail is an XmlNode if on the server you have
        // provided a complex XML you would be able to fetch it here
    }
}

InputException, FatalException NoFilesFound, , . - SoapException, , .

+7

All Articles