Consuming a Web Service

Team Fly 

Page 323

Not surprisingly, ASP.NET has facilities for caching data and for allowing you to define how long it remains cached. Here's how to do it.

Assume that you want to cache the response to your Headlines Web service for a half hour, then refresh it. If you were providing headline news service that you wanted to update every half hour, this would be the way to do it.

 <WebMethod(Description := ''Number of times this service has been accessed", _         CacheDuration := 1800, _         MessageName := "Headlines")> _     Public Function Headlines() As String 

The CacheDuration is expressed in seconds, and in this example, the first time this Web service is called, the response is calculated and returned to the client, but is also placed into the cache. For the next 30 minutes, any subsequent calls are not calculated. Instead, the cached response is merely sent to the clients. Obviously, this technique will often improve response time and Web service performance.

Consuming a Web Service

In the previous example, you saw how to write and test a Web service. Now let's move to the other side and see how to consume a Web service.

Later in this chapter you'll see how UDDI, WSDL, and other initiatives facilitate the publication, discovery, and consumption of Web services. For now, though, let's create a quick example that illustrates how, from within VB.NET code, you would go about consuming a Web service. The first step is to add a Web reference to a VB.NET project. For this example, you'll consume the Web service from within a Windows-style project.

Create a new VB.NET Windows-style project by double-clicking that icon in the New Project dialog box. Add a TextBox to the form, then choose Projectimage Add Web Reference. You see what looks like a streamlined browser window (shown in Figure 13.3) where you can search for Web services and see details about them, such as the parameters they expect when you submit a message to them for processing.

For this example, you want to contact and consume the Web service you created in the previous example in this chapter. Click the Web Services On The Local Machine link and VB.NET will provide you with a list of all the .ASMX files on your machine. Scroll past any QuickStart or other sample services until you locate Service1 (the default name VB.NET gives new Web services you write—we didn't change this default name in the previous example). You also see a path, like this:

http://localhost/services/Service1.asmx

as the "URL" for your service. Just to be sure that you have the correct service, click the Service1 link, as shown in Figure 13.4, to see that this is the correct service. You should see this description:

 Reverses the words in a submitted string 
 The following operations are supported. For a formal definition, please review the  Service Description. 
 ReverseWords 
Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

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