April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Book of VB .NET: Web Services, Part 2

  • April 5, 2002
  • By Matthew MacDonald
  • Send Email »
  • More Articles »

Excerpted with permission from The Book of VB .NET, by Matthew MacDonald, No Starch Press, Copyright 2002.

Consuming a Web Service

At this point, you've seen your Web Service in action, but you haven't usefullyincorporated it into another program. Clearly, if users had to rely on the InternetExplorer test page, they would be better off with a full-featured Internetapplication!

In this section you'll learn how to create a Web Service client in VB .NET.Best of all, you'll learn how to get Visual Basic .NET to create all the infrastructurecode you need.

The Proxy Class

Web Service clients communicate with Web Services through a proxy class thatyou can create automatically with the .NET framework. The proxy class translatesyour actions into the appropriate SOAP calls, and then interacts with theWeb Service. The whole process is seamless, so you might not have even realizedit was happening if you hadn't seen the following diagram:

Figure 13-5: Web Service interaction

Adding a Web Reference

To create the proxy class, you need to add a web reference in Visual Basic .NET.First, add a new Windows application project to the current solution by rightclickingon the Solution Explorer and selecting Add New Project. This projectshould not be created in the virtual directory where the Web Service is locatedbecause it will not be hosted on the web server.

Then, right- click on the new project and select Set As StartUpProject. Now,right-click one more time, and choose Add Web Reference. The Add Web Referencewindow will appear (Figure 13- 6).

Figure 13- 6: Adding a Web Reference

To add a web reference, you need to supply a discovery file. A discovery fileis the first step in the interaction process; it's where an application "discovers"what Web Services are available, what methods they have, and what informationthey require and return.

You may have noticed that Visual Basic .NET automatically added a .vsdiscofile to your Web Service project. This is the file you now need to select. Clickthe Web References on Local Server link. On the right, a list will appear of thediscovery files residing on your current computer. If you have already created anASP.NET application, you may find more than one discovery file. Choose theone that is in the appropriate directory.

Once you have made your selection, information will appear about the WebServices specified by the discovery document you've chosen. You can click onthe View Contract button to see all the WSDL information again, or click on theAdd Reference button to link to this Web Service and generate the requiredproxy class automatically.

Inspecting the Proxy Class

Visual Basic .NET hides the proxy class it creates from you, because you don'treally need to see it or modify it directly. However, it's always a good idea topeek under the hood of an application and get a better understanding of what'sreally happening along the way.

To see the proxy class, select Project Show All Files from the menu. Then,look for the folder under Web References with the .WSDL file. Double- click toexpand this entry, and you'll see the Visual Basic proxy file (typically calledReference.vb), as shown in Figure 13-7.

Figure 13- 7: The hidden proxy class

This file includes all the methods from your WebService, in slightly modifiedform. It also includes the ClientPackageInfo class that you need to use toretrieve the information from the GetPackageInfo method. Interestingly, VisualBasic .NET is intelligent enough to leave out the Package class, so the client willhave no idea that the Web Service uses this class internally and won't be able tosnoop out its internal details.

Take a quick look at the modified version of the GetPackageInfo function containedin the proxy class. (The attributes that precede it are not included here.)

Public Function GetPackageInfo(ByVal TrackID As String) As ClientPackageInfo  Dim results() As Object = Me.Invoke("GetPackageInfo", New Object() {TrackID})  Return CType(results(0), ClientPackageInfo)End Function

This function converts the TrackID input string into a generic object, andretrieves the result as a generic object, which is then converted into the appropriateClientPackageInfo object. In between, it accesses the Web Service through theappropriate SOAP request and waits for the response, although all this is takencare of automatically in the Me.Invoke method, with the help of the .NET attributesthat provide additional information to the Common Language Runtime.

Another interesting aspect of the proxy class is the constructor, which setsthe URL. If you change the location of your Web Service, you can modify thismethod, rather than regenerating the proxy class. Notice, however, thatlocalhost is specified, regardless of the name of your computer. Localhost is a"loopback" alias that always points to the current computer.

Public Sub New()  MyBase.New  Me.Url = "http://localhost/WebService/PostalWebService.asmx"End Sub

Using the Proxy Class

The proxy class is the key to using a Web Service. Essentially, you create aninstance of this class, and call the appropriate methods on it. You treat theproxy class as though it were a local class that contained the same functions andmethods as the Web Service.

Add the following code to the Load event of your startup Windows Form:

Dim ServiceInstance As New localhost.PostalWebService()Dim PackageInfo As New localhost.ClientPackageInfo()PackageInfo = ServiceInstance.GetPackageInfo("221")MessageBox.Show("Received the delivery date: " & PackageInfo.DeliveryDate)

Now run the application. If Visual Studio .NET loads up the Web Serviceweb page, you have to stop the project and set the Windows application as thestartup project, using the right-click menu in the Solution Explorer window.If you have configured everything correctly, you'll see the window inFigure 13- 8.

Figure 13- 8: The Web Service results

Once again, the technical details are pretty sophisticated, but the actualimplementation is hidden by the .NET framework. Calling a Web Service is aseasy as creating one, once you have set up the web reference.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel