Web Services in Action


ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 28.  Introduction to XML Web Services

Although Web Services and their use are relatively new, many businesses are beginning to understand the impact of being able to expose functionality for use in distributed applications across the Web. As you develop your own applications, you'll want to consider how you can take advantage of this technology. If you're thinking about ways you might use Web Services, here are some services you might want to create or consume:

  • Address normalization functions

  • Currency exchange rate converter

  • Credit card authorization

  • Shipment status

  • Order confirmations

  • Stock quote lookups

  • Catalog/product information

  • Weather information

Obviously, the list is endless. In any situation where you might want programmatic access to information on a remote computer, XML Web Services can provide the functionality you need.


Although you'll spend time creating and consuming your own XML Web Services in the next chapter, it's a good idea to "get your feet wet" and work through a simple example first, before digging into the more complex examples in the next chapter. In this section, you'll create a page that consumes an existing XML Web Service. We won't explain any of the details here you'll learn what's going on in the next chapter.

Because this example uses an XML Web Service that we don't control, we can't guarantee that it will be available (or at the same location) or provide the same functionality by the time you try it. We've included this particular example here because we know the service's author and believe that he intends to maintain the service indefinitely. Obviously, we can't predict the future, but we hope you'll be able to try out this service before creating your own in the next chapter.


This example requires a live Internet connection. Without the ability to retrieve the Web reference and then contact the XML Web Service, you'll be relegated to the sidelines, following the example and its figures without retrieving the actual data.

In this example, you'll create a page that allows you to enter a United States ZIP code and retrieve the current temperature (in Fahrenheit degrees) for the specified location. It's certainly not an Earth-shattering use of the technology, but it does show off the capabilities of consuming XML Web Services from within .NET applications.

Follow these steps to create a simple page that consumes an XML Web Service:

  1. Create a new ASP.NET Web project or add a new page to an existing project.

  2. Add Label, TextBox, and Button controls to the page. (For this simple demo, simply use the default names for the controls.) Delete the contents of the Text property for the Label control.

  3. Select the Project, Add Web Reference menu item.

  4. In the Add Web Reference dialog box, shown in Figure 28.3, click the Test Microsoft UDDI Directory link.

    Figure 28.3. Use the Add Web Reference dialog box to add a reference to an XML Web Service.


  5. In the Business Name text box, enter vbws (the name of the company providing the sample service) and then click Search.

  6. Once you see a list of available services in the left pane, select the Weather Service Registered Through VS .NET link.

  7. Under the tModel label, select the Weather Service Registered Through VS .NET WSDL-Interface link (see Figure 28.4).

    Figure 28.4. Select the specific service from the list of available services.


  8. Once you see the WSDL information in the left pane, click the Add Reference button (see Figure 28.5).

    Figure 28.5. Once you've download the WSDL (the XML Web Service contract information), you can add the reference to your project.


  9. Once you've added the Web reference, it shows up in the Solution Explorer window (in this case, as com.learnxmlws.www). Right-click the new reference, select Rename from the context menu, and rename the reference to vbws.


    You don't really have to rename the Web reference. We've added this step here because you'll need to interact with this object programmatically, and the shorter name is easier to manage from within your code.

  10. Double-click the button control on your page, bringing up the code editor. Modify the Button1_Click procedure so that it looks like this:

     Private Sub Button1_Click( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) _  Handles Button1.Click   Dim ws As New vbws.WeatherRetriever()   Label1.Text = ws.GetTemperature(TextBox1.Text) End Sub 
  11. In the Solution Explorer window, right-click your page and select Build and Browse from the context menu.

  12. Enter a U.S. ZIP code into the text box (enter 98052 to retrieve the temperature at the Microsoft home office) and then click the button.

  13. After a few seconds, you should see the current temperature appear in the label control (see Figure 28.6).

    Figure 28.6. The WeatherRetriever service does its job.


What happened here? When you added a Web reference to your project, you added a few new files (and some code) to your project so that you could interact with the remote service programmatically. In your code, you called a method of the remote service, passing in a parameter. The .NET Framework stepped in and created XML to send to the service, which expects a request formatted using the SOAP specification. The .NET Framework sent the XML information remotely and waited for a response. The service did its job, retrieving the temperature for the requested area, and returned XML back to your project. The .NET Framework again stepped in, retrieving just the results you needed from the XML sent back from the service, and returned that string as the return value of the method you called.

All in all, this was hardly a difficult task for you, the developer! The .NET Framework did a lot of work on your behalf, but you managed to call a remote service as if it were a local object.


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234

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