Global Exception Handling

Ask a Question related to ASP.NET Web Services, Design and Development.

  1. #1

    Default RE: Global Exception Handling

    Exceptions thrown by an XML Web service method created using ASP.NET are
    sent back to the client in the form of a SOAP fault. A SOAP fault is a
    <Fault> XML element within a SOAP message that specifies when an error
    occurred. When passing a SOAP fault, ASP.NET follows the method prescribed
    for propagating errors back to a client by the SOAP specification. The SOAP
    <Fault> XML element contains details such as the exception string and the
    source of the exception. For details on SOAP faults, see the W3C Web site
    ([url]http://www.w3.org/TR/SOAP[/url]).

    Fortunately, both clients and XML Web services created using ASP.NET do not
    populate or parse the <Fault> XML element directly, but rather use the
    common design pattern for throwing and catching exceptions in the .NET
    Framework. An XML Web service can either throw an exception specific to the
    problem, such as an ArgumentOutOfRangeException or SoapException. Either
    way, ASP.NET serializes the exception into a valid SOAP message by placing
    the exception into a SOAP fault element. When the SOAP message is
    deserialized on an ASP.NET client, the SOAP fault is converted to a
    SoapException exception, with the exception details placed in the Message
    property. A client can thus set up a Try/Catch block to catch a
    SoapException.

    A Web application can be comprised of multiple XML Web services, however
    the Application_Error event within the Global.asax file cannot be used for
    global exception handling. The HttpHandler for XML Web services consumes
    any exception that occurs while an XML Web service is executing and turns
    it into a SOAP fault prior to the Application_Error event is called. Build
    a SOAP extension to process XML Web service exceptions in a global
    exception handler. A SOAP extension can check for the existence of an
    exception in the ProcessMessage method. Within the ProcessMessage method,
    check the Exception property of the SoapMessage passed when the Stage
    property is set to AfterSerialize.

    For more informaiton abour soap extensions, you can refer to:

    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm[/url]
    l/cpconAlteringSOAPMessageUsingSOAPExtensions.asp

    Generally, we can catch the error in AfterSerialize with following code:

    public void WriteOutput(SoapMessage message)
    {


    if (message.Exception != null)
    {
    MemoryStream ErrMsg= new MemoryStream();
    StreamWriter ErrWriter = new StreamWriter(ErrMsg);

    ErrWriter.WriteLine("...");
    ErrWriter.Flush();
    ErrWriter.Close();
    Copy(ErrMsg, oldStream);
    }





    }


    Hope this help,

    Luke
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    [MSFT] Guest

  2. Similar Questions and Discussions

    1. Handling error with exception
      Do perl have handling error (exception) mechanism? for example: java use : try{...} catch{...} Thanks
    2. SoapExtension for Global Exception handling; but prevent exception from propagating!!
      Hi, I wrote a SoapExtension for Global Exception handling in Webservice and it works!!!! Now i want to catch the exception that happen in the...
    3. Global Error handling in Applicatio_Error() of Global.asax
      Hi all, For a web application if we are using web farm, and if i want to do Global Error handling can i use Applicatio_Error() method in...
    4. Exception Handling.
      Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if...
    5. Exception handling problem
      I override protected void Application_Error(Object sender, EventArgs e) I have code that will read the last Exception message and redirect to a...
  3. #2

    Default RE: Global Exception Handling

    Hello,

    With classic ASP, wen can catch the exceptions in Application_Error. Could
    you please tell me how you implement web service with classic ASP? When we
    mention Web Service, we always mean ASP.NET web service. Is the ASP
    application is a client of your web service?

    If I have any misunderstanding, please feel free to let me know.

    Luke
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    [MSFT] Guest

  4. #3

    Default RE: Global Exception Handling

    Hi Luke

    Sorry for the confusion. The web service is written in C#, it is a classic ASP application that is consuming the web service. I have a good handle on catching errors in the classic ASP side, it is the throwing of the errors from the C# web service where I am having problems

    When there is an error in the C# web service there is no error bubbled down to classic ASP. This is OK, I am completely comfortable with checking the return string of the web method for an error. The problem is that the return from the web service, when there is an error in the web service, does not contain the error in an XML document. It is being returned as pure text, no XML tags

    What I would like to do is within the C# web service, in a global manner, be able to format the error in XML so that the classic ASP application can check the return from the web service for an error in the string. I am hoping to be able to handle this globally within the webservice so I can avoid having to try, catch, and rethrow in every webmethod just to get the error to return as an XML representation of itself. I was under the impression that an error from the webservice would be returned as XML, but it isn't. It is just returning as plain text (no XML tags)

    Thanks again
    Cindy
    Cindy Guest

  5. #4

    Default RE: Global Exception Handling

    Hi Cindy,

    Thank you for the information. Normally, when there is a exception in web
    method, it will return a SOAP message like:

    <soap:fault>
    ...
    </soap:fault>

    It is well formed XML string. Did you use SOAP client in classic ASP to
    access C# web service? If so, you will get the exception message directly
    like:

    System.Web.Services.Protocols.SoapException: Server was unable to process
    request. ---> System.DivideByZeroException: Attempted to divide by zero.

    Is this why you tell you got plain text instead of XML?

    Luke
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)


    [MSFT] Guest

  6. #5

    Default RE: Global Exception Handling

    Hi Luke

    I did not use SOAP directly from classic ASP to access the C# web service. Here is a code snippet from the ASP application that accesses the web service. If there is an error, it is returned in the xmlhttp.ResponseText as plain text (no XML tags)

    xmlhttp.Open "POST", serviceUrl, Fals
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded
    xmlhttp.send queryStringValue
    rtnValue = xmlhttp.ResponseTex

    Cindy Guest

  7. #6

    Default RE: Global Exception Handling

    In this way, the ResponseText will only contains the exception message
    indeed. To make sure if there is an error from web service, you may check
    xmlhttp's Status and StatusText. If there is an error from web service, the
    Status should be an error number other than 200.

    If you need customized error message from web service, you may refer to my
    first message. The SoapExtesion should be able to achieve this.

    Luke
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)



    [MSFT] Guest

  8. #7

    Default RE: Global Exception Handling

    Thank you! That will work great for centralizing the error handling in the classic ASP code. :)
    Cindy Guest

  9. #8

    Default Re: Global Exception Handling

    Very informative post. It helped me lot in understanding Exception handling in c#.net. Thanks for sharing with us and I would like to appreciate you please keep writing. While I am searching article on this topic over the internet then I was found another article too which also explained very well on exception handling in c#.Net, please check that url post at once......
    http://mindstick.com/Articles/de21d4fc-c6ea-4ae8-91db-874b727e5e6c/?Exception%20Handling%20in%20C%20Sharp

    Thanks everyone for your precious post.
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139