Calling Web Services


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 18.  Using and Implementing Web Services

Calling Web Services

The benefit of open standards is that other language and tool vendors can and probably will support Web Services. Because Visual Basic .NET hides much of the marshaling between client and server in the background, you can use Web Services as if they were local objects.

Taking a hint from the "Four Facets of Web Services" from the preceding section, the first thing you will need to do to use a Web Service is to look in a directory for the Web Service. (There are several Web Service examples on this book's Web site, and we will cover how you can use and experiment with those later in this chapter.)

Adding a Web Reference

To use a Web Service, you need to add a reference to the service. The Reference folder in the Solution Explorer has a context menu that has two menu items (see Figure 18.1). The first menu item supports adding references to .NET assemblies, COM objects, and Visual Studio .NET projects. The second menu is used to add Web references, that is, references to Web Services.

Figure 18.1. Adding Web references to a project.


When you choose Add Web Reference, the Add Web Reference dialog box (see Figure 18.2) is displayed. This dialog box provides links to the Microsoft UDDI Directory, a Test Microsoft UDDI Directory, and Web References on Local Server. We will use the latter for our initial demonstration.

Figure 18.2. The Add Web References dialog box.


To add a Web reference to a project, follow these steps:

  1. Create a Windows application.

  2. Click View, Solution Explorer to open the Solution Explorer.

  3. Right-click over the References folder in the Solution Explorer and choose Add Web Reference.

  4. In the Add Web Reference dialog box, choose Web References on Local Server.

  5. If there are any .vsdisco files on your local IIS server, a list of available Web Services will be displayed in the available references list. For our purposes we will assume that WebService1. vsdisco exists.

  6. Choose the WebService1.vsdisco file and click the Add Reference button. This will add a Web References folder to the Solution Explorer (see Figure 18.3).

    Figure 18.3. The Solution Explorer after a Web reference has been added.


When a Web Service is compiled, a .DLL is created. After you have added the Web Reference, you are ready to use any Web Services defined. Using the Web Service DLL is like using any other DLL after the reference has been added. You can declare instances of classes defined in the Web Service DLL just as you would any classes defined in a vanilla class library.

Using Windows Forms Applications

Web Services can be used for Windows or Web development. Although it is assumed that a Web application will have a connection to the Internet, the implication is that a Windows application calling a Web Service will be connected too.

WebService1.dll added in the preceding section contains a single WebService instance named Service1. There is only one public WebMethod in the class, named HelloWorld.

After the service has been added, we can declare an instance of the WebService class, Service1, and invoke any methods that have the WebMethodAttribute. Using our example WebService and WebMethod, Listing 18.1 demonstrates creating the WebService and invoking the WebMethod.

Listing 18.1 Instantiating a WebService and invoking a WebMethod from a Windows application
  1:  Public Class Form1  2:  Inherits System.Windows.Forms.Form  3:   4:  [ Windows Form Designer generated code ]  5:   6:  Private Sub Button1_Click(ByVal sender As System.Object, _  7:  ByVal e As System.EventArgs) Handles Button1.Click  8:   9:  Dim Service As New localhost.Service1()  10:  MsgBox(Service.HelloWorld())  11:   12:  End Sub  13:  End Class 

As you can determine from line 2 of the listing, the code resides in a Windows Form. Lines 6 through 12 implement a Button.Click event handler. Line 9 instantiates the WebService, and line 10 invokes the WebMethod HelloWorld.

We will look at implementing WebService and WebMethod later in this chapter.

Using Web Forms Applications

Roughly the equivalent solution can be contrived using the same Web Service in an ASP.NET (or Web) application. To use a Web Service in an ASP.NET application, create a new ASP.NET application in Visual Studio .NET. Add a Web reference to the ASP.NET application pointing to the same Web Service, WebService1.


You also can add a Web reference by choosing Project, Add Web Reference.

The demo application, FindWebService.vbproj, contains a single WebForm with a Button painted from the Web Forms page of the toolbox. (Refer to Chapter 19, "ASP.NET Web Programming," for more on ASP.NET and Web Forms.) The Button control was double-clicked to add a Click event handler. Just as in Listing 18.1, an instance of the Web Service was created and used in the Click event handler. Listing 18.2 demonstrates using the Web Service from an ASP.NET page, and Figure 18.4 shows the page in a browser.

Figure 18.4. ASP.NET application demonstrating a simple Web Service.


Listing 18.2 Instantiating and calling a Web Service from an ASP.NET application
  1:  Public Class WebForm1  2:  Inherits System.Web.UI.Page  3:  Protected WithEvents Button1 As System.Web.UI.WebControls.Button  4:   5:  [ Web Form Designer Generated Code ]  6:   7:  Private Sub Button1_Click(ByVal sender As System.Object, _  8:  ByVal e As System.EventArgs) Handles Button1.Click  9:   10:  Dim Service As New localhost.Service1()  11:  Response.Write("<i>" & Service.HelloWorld() & "</i>")  12:   13:  End Sub  14:  End Class 

Notice the striking similarities between the Visual Basic .NET in the Windows application in Listing 18.1 and the Visual Basic .NET in the ASP.NET application in Listing 18.2. We can't show a modal dialog using MsgBox in Internet Explorer, so the output from WebForm1 is rendered using HTML.

The Response object is implemented using the Request and Response pattern (that exists in at least two development tools, Delphi and Visual Basic .NET). For now, just think of the Response object as roughly the equivalent of the Console object for console applications. The Response object is a public property of WebForm and is used to send information back to the Web server; we will cover Response in greater detail in Chapter 19.


The WebForm class is subclassed from System.Web.UI.Page. Look in the Page class for more information on Web Forms.

The HTML is easy. The <i> and </i> tags italicize the text between the tags. Combined, the Response.Write statement yields the text portion of the Web page shown in the browser in Figure 18.4.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: