Previous Page
Next Page

Consuming Web Services
NOTE
Consuming a Web service is nearly as easy as writing one. The Microsoft .NET Framework and Visual Studio have handy utilities that generate proxies for Web services automatically. Visual Studio is not the only way to consume Web services. May modern applications have ways to consume Web services. Web services are meant to be platform independent, and most modern computing platforms support consuming Web services.

The following example illustrates consuming the QuoteService via a small command line program.

Use the QuoteService
  1. Add a new subproject to the solution. Make the new project a console application by selecting the Console Application template. Name the project ConsumeWebService.

    Graphic
  2. Highlight the ConsumeWebService project in the solution explorer and right-click. Select Add Web Reference from the local menu.

    Graphic

    Click on the Web services in this solution link:

    Graphic

    Select the QuoteService link. You should see all the available methods. Click the Add Reference button. Visual Studio will generate a proxy for you.

    Graphic
    TIP

    Another way to generate a proxy is to surf to the Web service, ask for the WSDL manually, and the run the WSDL code through a utility named WSDL.exe.

    For example, the following query string fetches the WSDL:

    http://localhost/WebServiceORama/QuoteService.ASMX?wsdl

    Save the document sent back by the service and run it through the WSDL command line utility:

    C:\>WSDL quoteservice.wsdl

    This will generate a C Sharp file you may use in your application. (WSDL has command line parameters for generated proxies in VB, too.)

    TIP
    At this point, a number of Web services are already available. To get an idea of what's available, poke around some of the other links in the Add Web Reference dialog box. For example, the Universal Description, Discovery and Integration repository (UDDI) lists a number of services. It's interesting to poke around and see what's there.
  3. The namespace in which the new proxy lives reflects the origin of the Web service. Because this proxy came from a service on this machine, the namespace is localhost. To access types from the new service proxy, you must preface the types with localhost. You could also include the localhost in the using block where the other namespaces are imported into the project.

  4. The name of the QuoteService proxy is (strangely enough) QuoteService. You instantiate it like you would any other class. When you call methods, the proxy will wrap the call in a SOAP envelope and send the request to the destination specified within the proxy. Try making calls to GetAQuote, AddQuote, and GetAllQuotes, as shown below.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    
    namespace ConsumeWebService
    {
       class Program
       {
    
          static void Main(string[] args)
          {
             localhost.Quote quote;
             localhost.QuoteService quoteService;
    
             quoteService = new localhost.QuoteService();
    
             for(int i = 0; i < 10; i++)
             {
                quote = quoteService.GetAQuote();
                System.Console.WriteLine("Quote: " + quote._strQuote);
                System.Console.WriteLine( "Originator: " +
                                  quote._strOriginatorFirstName + "" +
                                  quote._strOriginatorLastName);
                System.Console.WriteLine();
             }
          }
       }
    }

    When you run the application, you should see some output like this:

    Graphic

    Remember, the beauty of Web services is that they're not tied to a particular platform. The previous example shows how to consume the QuoteService (which is an ASP.NET application). However, Visual Studio builds proxies for any normal Web service. You could easily have searched around for other sites implementing Web services for which Visual Studio will build you a suitable proxy.


Previous Page
Next Page