Throwing an Exception within a Web Service

As you learned in Chapter 1, when a web service encounters an error, the service will often generate (throw) an exception. Programs that interact with web services should do so within a Try-Catch block that lets the program detect and respond to the exceptions.

The following ExceptionDemo web service supports two methods, named Bad and Good. When your program calls the Good method, the web service will return a string that contains a quote:

string Good() string Bad()

However, when your program calls the Bad method, the web service will generate an exception. To create the ExceptionDemo web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name ExceptionDemo. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

    [WebMethod] public string Good() {      return "Great spirits have always encountered " +       "violent opposition from mediocre minds" +       " ... Albert Einstein"; } [WebMethod] public string Bad() {      Exception ex = new Exception("Fatal Exception in Service");      throw(ex); }

  4. After you enter the code, select the Build menu Build Solution option to create the service.

The service’s Good method is quite straightforward—the service simply returns a string that contains the quote. The Bad method, in contrast, first creates an Exception variable. Then, using a throw operation, the method generates the exception. Within the services you write, your code can use exceptions to signal a variety of events to the calling program. When you create an exception, you can customize the message (information) that corresponds to the exception.

Putting the Exception Demo Web Service to Use

The following C# program, CatchException.cs, displays a form that contains buttons, as shown in Figure 2.14, which you can select to call the ExceptionDemo web service’s Good and Bad methods.


Figure 2.14: Directing a program to catch and handle or to not handle the exceptions a web service generates

Using the buttons, you can direct the program to catch and respond to the exception the Bad method generates or you can direct the program to not catch the exception. If you direct the program to not handle the exception, the program will fail when the exception occurs and will display a dialog box similar to that shown in Figure 2.15.

click to expand
Figure 2.15: If a program fails to catch an exception thrown by a web service, the program will fail.

To create the CatchException.cs program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields, type CatchException. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the buttons and text box previously shown in Figure 2.14 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/ExceptionDemo/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

          private void button1_Click(object sender, System.EventArgs e)       {          localhost.Service1 Obj = new localhost.Service1();             try             {                textBox1.Text = Obj.Good();             }             catch (Exception Ex)             {                textBox1.Text = Ex.Message;             }       }       private void button2_Click(object sender, System.EventArgs e)       {          localhost.Service1 Obj = new localhost.Service1();          try          {             textBox1.Text = Obj.Bad();          }          catch (Exception Ex)          {             textBox1.Text = Ex.Message;          }       }       private void button3_Click(object sender, System.EventArgs e)       {          localhost.Service1 Obj = new localhost.Service1();             textBox1.Text = Obj.Bad();       }

As you can see, within the Bad (Caught) button handler, the code calls the Bad method within a try-catch block, which lets program detect and respond to the exception. In contrast, within the Bad (Not Caught) button handler, the code simply calls the method. Because the program does not detect and respond to the exception, the program will fail.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net