Remembering What Makes a Good Web Service

Because a web service requires the exchange of network messages and because the server that executes the web service may be busy performing other tasks (thus delaying its execution of the service you desire), a web service will execute much slower than a standard function.

As briefly discussed in Chapter 1 in the section “Understanding Operations That are Well Suited for Web Services,” many operations are not geared for implementation as a web service. The network overhead required to execute the remote service slows down the processing too much. The Hello web service, for example, is an operation you should not implement as a web service within your programs. The operation the web service performs simply does not justify its overhead.

However, by changing the service’s processing slightly, you can create a service whose processing outweighs the cost of the network operations. The following GreetSpecific web service returns a “Hello World” based on a specific language, such as English, Spanish, German, and so on. To perform its processing, the web service supports the following methods:

string English() string Spanish() string French() string German()

To create the 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 GreetSpecific. 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 English() {   return "Hello World"; } [WebMethod] public string Spanish() {   return "Hola Mundo"; } [WebMethod] public string German() {   return "Hallo Welt"; } [WebMethod] public string French() {   return "Bonjour Monde"; }
  4. After you enter the code, select the Build menu Build Solution option to create the service.

As you can see, the web service’s methods are quite simple. Each method simply returns a string that contains a greeting. By supporting multiple methods, the service increases the number of operations it supports.

Note 

You may want to extend the number of languages the server supports. To do so, simply add a method for each language you desire. To translate the “Hello World” message, you can use an online translator, such as the one you will find at www.systransoft.com.

Putting the GreetSpecific Web Service to Use

The following Visual Basic .NET program, ShowGreetings.vb, displays a form with buttons for specific languages. When you click a button, the program will display the “Hello World” message in the corresponding language, as shown in Figure 2.4.


Figure 2.4: Using the GreetSpecific web service to display a message in specific languages

To create the ShowGreetings.vb 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 Visual Basic Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields, type ShowGreetings. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.

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

  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/GreetSpecific/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:

    Dim GreetObject As New localhost.Service1()     Private Sub Button1_Click(ByVal sender As System.Object, _ Ä   ByVal e As System.EventArgs) Handles Button1.Click         Try             TextBox1.Text = GreetObject.English()         Catch Ex As Exception             TextBox1.Text = Ex.Message         End Try     End Sub     Private Sub Button2_Click(ByVal sender As System.Object, _ Ä   ByVal e As System.EventArgs) Handles Button2.Click         Try             TextBox1.Text = GreetObject.Spanish()         Catch Ex As Exception             TextBox1.Text = Ex.Message         End Try     End Sub     Private Sub Button3_Click(ByVal sender As System.Object, _ Ä   ByVal e As System.EventArgs) Handles Button3.Click         Try             TextBox1.Text = GreetObject.French()         Catch Ex As Exception             TextBox1.Text = Ex.Message         End Try     End Sub     Private Sub Button4_Click(ByVal sender As System.Object, _ Ä   ByVal e As System.EventArgs) Handles Button4.Click         Try             TextBox1.Text = GreetObject.German()         Catch Ex As Exception             TextBox1.Text = Ex.Message         End Try     End Sub

The program defines event handlers for each of the form’s buttons. Within each handler, the code calls a specific web service method and assigns the result to a text box. To reduce the amount of code in each handler, the program creates the web service object, in this case named GreetObject, outside of the methods (creating a global variable each method can access).




. 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