Chapter 2: Creating Your First Web Services

In Chapter 1, “Taking Web Services for a Test Drive,” you used Visual Studio .NET to create programs and ASP.NET pages that let you test drive a variety of web services written by other programmers. In this chapter, you will use Visual Studio .NET to create your own web services. Across the Web, programmers create web services using other programming environments, such as Java. As you will learn in this chapter, Visual Studio .NET makes it very easy for you to create web services.

In Chapter 1, you learned that to use a web service, your programs simply called one of the web service’s methods, passing to the method the required parameters. In this chapter, you will create a web service by defining the methods that perform the service’s processing. You will create a myriad of web services. You will learn how to pass parameters of different types to a service, how to change parameter values within a service, and how to take advantage of arrays and structures to store the data that a service manipulates or returns. Like Chapter 1, this is a hands-on chapter. After you implement a service, take time to experiment with the service by changing the processing the service performs. In so doing, you will increase your understanding of the process of creating and using web services.

Getting Started with the Hello Web Service

The first C program that most C programmers write uses the printf function to display a hello message, similar to that shown by the program Hello.c:

#include <stdio.h> void main(void) {   printf("Hello, world!"); }

When the programmers move on to C++, they often create a similar program, as shown here:

#include <iostream.h> void main(void) {   cout << "Hello, world!"; }

It seems fitting that the first web service you create should resemble the well-traveled “Hello” program. In this section, you will use Visual Studio .NET to create a web service that returns a string containing the message “Hello World.” Then you will create a Visual Basic .NET based ASP.NET page and a C# program that use the service.

Just as you can use (call) web services from programs written in a variety of programs, you can also create web services using myriad programming languages. To create the Hello web service using Visual Studio .NET, 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 ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name Hello. Select OK. Visual Studio .NET will display a page, as shown in Figure 2.1, 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.

    click to expand
    Figure 2.1: Each time you create a web service, Visual Studio .NET displays a page onto which you can place the service’s components.

To help you create web services quickly, Visual Studio .NET provides sample code (a template) you can use to define your service’s methods. As it turns out, the code template provides a method that returns the “Hello World” message. Visual Studio .NET comments out the code as shown here:

    '<WebMethod()> Public Function HelloWorld() As String     ' HelloWorld = "Hello World"     ' End Function

In this case, to use the code, you must simply remove the comment character (the single quote) from the start of each line of code:

    <WebMethod()> Public Function HelloWorld() As String     HelloWorld = "Hello World"     End Function 

When you create web services within Visual Studio .NET, you must precede each of the service’s methods with the <WebMethod()> that tells Visual Studio .NET to perform operations behind the scenes when you build your service that makes the method callable from across a network.

Normally, when I create web service methods, I begin by cutting and pasting the template code Visual Studio .NET provides. I then change my copy of the code to implement the method’s processing (changing the method name, parameter list, return value, and so on, as required).

After you have uncommented the template code, select the Build menu Build Solution option. Visual Studio .NET will build your Hello web service. That’s all there is to it. Select the File menu Close Solution option to close your web service. You are now ready to create an ASP.NET page that puts the Hello service to use.

Putting the Hello Web Service to Use

The ASP.NET page UseHello.aspx, which you can view from this book’s companion website (www.sybex.com), displays a single button that contains the message, “Click Here to Call the Hello Web Service.” After you click the button, the page will call the Hello service’s HelloWorld method, displaying the value the service returns, as shown in Figure 2.2.

To create the UseHello.aspx ASP.NET page, 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 ASP.NET Web Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name UseHello. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

    click to expand
    Figure 2.2: Calling the Hello web service from within an ASP.NET page

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

  4. As you learned in Chapter 1, to use a web service you must assign a Web Reference to the program that corresponds to the object. To do so, 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 the URL of a special file (called the WSDL file) that describes the web service. In this case, type localhost/Hello/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 Sub Button1_Click(ByVal sender As System.Object, _ Ä   ByVal e As System.EventArgs) Handles Button1.Click         Dim WebServiceObject As New localhost.Service1()         Try             TextBox1.Text = WebServiceObject.HelloWorld()         Catch Ex As Exception             TextBox1.Text = "Exception in Web service: " & Ex.Message         End Try     End Sub

As you can see, the program defines an event handler that responds to the user’s button-click operation. Within the handler, the code defines an object named WebServiceObject that the program will use to interact with the web service. Then, within a Try-Catch block that lets the program detect an exception the web service might generate, the code calls the service’s HelloWorld method, assigning the result to the form’s text box. To run the ASP.NET page, simply select the Debug menu Start option.




. 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