Working with Errors in SOAP

While working with Web Services, you eventually will need to handle errors in a more constructive way. The question is, how do you want to handle the errors that pop up? The following error message is an example of what you would see if you did not have any mechanism for handling errors:

System.IndexOutOfRangeException: Exception of type System.IndexOutOfRangeException was
 thrown. 
at chap_13_c. TimeService.ErrorString() in 
c:inetpubwwwrootchap_13_cservice1.asmx.cs:line 38 

This error message is typical of what you would see if your Web Service ran into an error. To keep this from happening, Listings 13.11 and 13.12 show a simple example to catch the error and return it inside the contents of the SOAP message.

Listing 13.11. WebMethod Error (C#)
[WebMethod] 
      public object ReturnErrorString() 
      {
      // Catch the error then return it inside a SOAP message. 
                   string[] sret = new string[3]; 
                   try 
                   {
                         sret[0] = "one"; 
                         sret[1] = "two"; 
                         sret[3] = "three";//Error 
                   } 
                   catch(Exception e) 
                   {
                         sret[0] = e.Message; 

                   } 
                   return sret; 
            } 

Listing 13.12. WebMethod Error (Visual Basic .NET)
<WebMethod()> Public Function ErrorStringCatch() As String() 
   ' Catch the error then return it inside a SOAP message.Dim sret(3) As String 

        Try 
            sret(0) = "Hello" 
            sret(1) = "Bye" 
            sret(13) = "Testing" 'This will generate an error 


        Catch e As Exception 

            sret(0) = e.Message 

        End Try 

        ErrorStringCatch = sret 
    End Function 

If you look closely at the code in Listings 13.11 and 13.12, you will notice that we intentionally added a runtime error while adding the third item to the array.

Now when you run into an error, it will be returned inside the SOAP message. This is one way to approach handling errors and sending them back to the client, but this is not the preferred method for doing so. Instead, one of two things should be done. First, you cannot catch the exception that will send it back to the client to handle. Second, you can identify what the problem is inside the catch and try to resolve the problem or throw another exception that will be sent back to the client. This gives you the capability to identify the error inside your Web Service client and deal with it there. Otherwise, if you just returned the error message inside the array, you would need to check the first item of the array and try to identify whether it is an error message. If you did this, the return message might look something like Listing 13.13.

Listing 13.13. SOAP Return Values
<?xml version="1.0" encoding="utf-8" ?> 

<Object n1:type="ArrayOfString"Listing 13.12 xmlns:n1="http://www.w3.org/2001
/XMLSchema-instance" xmlns="Debugging_Asp.NET_webservices"> 
<string>Exception of type System.IndexOutOfRangeException was 
thrown.</string> 
   <string>Bye</string> 
   <string>xmlns:xsd="http://www.w3.org/2001/XMLSchema" n1:nil="true" /> 
</Object> 

What if you are working only with numbers? Then how do you go about sending back error messages? If you look at Listings 13.14 and 13.15, you will see that you can use the try-catch method and then rethrow the error.

Listing 13.14. Throw Exception (C#)
[WebMethod] 
      public object ReturnErrorString() 
      {
      // Catch the error then return it inside a SOAP message. 
                   string[] sret = new string[3]; 
                   try 
                   {
                         sret[0] = "one"; 
                         sret[1] = "two"; 
                         sret[3] = "three";//Error 
                   } 
                   catch(Exception e) 
                   {
                         Throw(e);//Return the exception to the client 

                   } 
                   return sret; 
            } 

Listing 13.15. Throw Exception (Visual Basic .NET)
<WebMethod()> Public Function ErrorStringCatch() As String() 

   ' Catch the error then return it inside a SOAP message.Dim sret(3) As String 

        Try 

            sret(0) = "Hello" 
            sret(1) = "Bye" 
            sret(13) = "Testing" 'This will generate an error 


       Catch e As Exception 

            Throw(e) 'Return the exception to the client 

        End Try 

        ErrorStringCatch = sret 
    End Function 

If you look at the Catch statement, the Throw method is being called to pass the error on to the client. By adding this line of code, you change the behavior of the Web Service dramatically. A simple change in one line of code can make a world of difference when it comes to debugging. Don’t forget that you need to use the try-catch statement on the client side as well, to be able to handle the exception being passed back by the server. To show how the client should be implemented, take a look at the code in Listings 13.16 and 13.17.

Listing 13.16. The try-catch Statement on the Client Side (C#)
private void Button2_Click(object sender, System.EventArgs e) 
             {
                    try 
                    {
                          localhost.Service1 s = new localhost.Service1(); 
                          s. ErrorStringCatch (); 

                    } 
                    catch(Exception ex) 
                    {
                          TextBox1. Text = ex.Message; 
                    } 

             } 

Listing 13.17. The try-catch Statement on the Client Side (Visual Basic .NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 Handles Button1.Click 
         Try 
            localhost.Service1(s = New localhost.Service1()) 
            s.ErrorStringCatch() 

            catch(Exception ex) 
            TextBox1. Text = ex.Message 
        End Try 

    End Sub 

In Listings 13.16 and 13.17, the user clicks the button to invoke the method of the Web Service. If there is an error on the server side, the Web Service passes back the error information, and it can be received in the catch statement.When an error does get caught, it is displayed in the text box that is on the page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset