Instantiating and Invoking Web Services



Instantiating and Invoking Web Services

After you have discovered a Web service and retrieved information about its interface, you can instantiate an object that represents that Web service and then invoke its methods. In this section you'll learn about two methods to integrate Web services in your applications, and you'll learn about testing a Web service as a consumer.

Creating Proxy Classes with the Web Services Description Language Tool (wsdl.exe)

The .NET Framework SDK includes the Web Services Description Language tool, wsdl.exe. This tool can take a WSDL file and generate a corresponding proxy class that you can use to invoke the Web service, as shown in Step-by-Step 4.8.

STEP BY STEP

4.8 Using the Web Services Description Language Tool

  1. Select Start, Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools, Visual Studio .NET Command Prompt. This opens a command prompt window and sets the environment up so that you can use any of the command-line tools from the .NET Framework SDK.

  2. Navigate to the folder that contains the WSDL file that you created in Step-by-Step 4.7.

  3. Enter the following command to create a proxy class to call the Airport Weather Web service:

    
    wsdl /language:CS /out:AirportWeatherProxy.cs AirportWeather.wsdl
    
    

    The tool reads the WSDL file and creates a new file named AirportWeatherProxy.cs.

  4. Add the AirportWeatherProxy.cs file to your Visual Studio .NET Windows application project by selecting File, Add Existing Item.

  5. Add a new Windows form to your Visual C# .NET project.

  6. Place a Label control, a TextBox control (txtCode), a Button control (btnGetWeather), and a Listbox control (lbResults) on the form. Refer to Figure for the design of this form.

  7. Double-click the Button control and enter the following code to invoke the Web service when the user clicks the Get Weather button:

    
    private void btnGetWeather_Click(object sender,
    
        System.EventArgs e)
    
    {
    
        // Connect to the Web service by declaring
    
        // a variable of the appropriate type
    
        // available in the proxy
    
        AirportWeather aw = new AirportWeather();
    
    
    
        // Invoke the service to get a summary object
    
        WeatherSummary ws = aw.getSummary(txtCode.Text);
    
    
    
        // And display the results
    
        lbResults.Items.Clear();
    
        lbResults.Items.Add(ws.location);
    
        lbResults.Items.Add("Wind " + ws.wind);
    
        lbResults.Items.Add("Sky " + ws.sky);
    
        lbResults.Items.Add("Temperature " + ws.temp);
    
        lbResults.Items.Add("Humidity " + ws.humidity);
    
        lbResults.Items.Add("Barometer " + ws.pressure);
    
        lbResults.Items.Add("Visibility " +
    
           ws.visibility);
    
    }
    
    
  8. Insert the Main() method to launch the form. Set the form as the startup object for the project.

  9. Run the project and fill in a value for the airport code. Click the Get Weather button. After a brief pause while the Web service is invoked, you'll see some information in the ListBox control, as shown in Figure. This information is delivered from the server where the Web service resides, as properties of the WeatherSummary object. The difference between this and the version in Step-by-Step 4.1 is that this code explicitly defines the objects that it uses rather than discovering them at runtime. The AirportWeather and WeatherSummary objects are proxy objects that pass calls to the Web service and return results from the Web service. This information is delivered from the server where the Web service resides, as properties of the WeatherSummary object.

Figure shows some of the command-line options that you can use with wsdl.exe. You don't need to memorize this material, but you should be familiar with the tool's overall capabilities. You can use either the path to a local WSDL or Disco file or the URL of a remote WSDL or Disco file with this tool.

Figure Command-line Options for wsdl.exe

Option

Meaning


/domain:DomainName

/d:DomainName

Specifies the domain name to use when connecting to a server that requires authentication.


/language:LanguageCode

/l:LanguageCode

Specifies the language for the generated class. The LanguageCode parameter can be CS (for C#), VB (for Visual Basic .NET) or JS (for JScript).


/namespace:Namespace

/n:Namespace

Specifies a namespace for the generated class.


/out:Filename

/o:FileName

Specifies the filename for the generated output. If this option is not specified, the filename is derived from the Web service name.


/password:Password

/p:Password

Specifies the password to use when connecting to a server that requires authentication.

/server

Generates a class to create a server based on the input file. By default, the tool generates a client proxy object.


/username:Username

/u:Username

Specifies the username to use when connecting to a server that requires authentication.

/?

Displays full help for the tool.

Using Web References

As an alternative to using the Web Services Discovery tool and the Web Services Description Language tool to create explicit proxy classes, you can simply add a Web reference to your project to enable the project to use the Web service. You've seen Web references several times in this chapter, starting in Step-by-Step 4.1.

In fact, there's no difference in the end result between using the tools to create a proxy class and adding a Web reference. Behind the scenes, the Web reference creates its own proxy class. To see this, click the Show All Files toolbar button in Solution Explorer, and then expand the Solution Explorer node for a Web reference. You'll see a set of files similar to that shown in Figure.

13. When you add a Web reference, the proxy class is automatically generated.

graphics/04fig13.jpg

The .disco and .wsdl files are the same files that would be generated by running the Web Services Discovery tool on the URL of the Web reference. The .map file is same as the .discomap file generated by the Web Services Discovery tool. The .cs file defines the proxy objects to be used with the Web service represented by this Web reference, as you can see by opening this file. The major difference between this file and the proxy that you generate with the Web Services Description Language tool is that the auto-generated file uses a namespace that is based on the name of the Web reference.

EXAM TIP

Why Use a Web Reference? The major benefit of using a Web reference (as compared to constructing proxy classes with the command-line tools) is that it's easier to update the proxy classes if the Web service changes. All you need to do in that case is right-click the Web Reference node in Solution Explorer and select Update Web Reference.


Testing a Web Service

If you'd like to test a Web service without building an entire client application, you can use a testing tool. Several such tools are easily available:

All three of these tools work in the same basic way: They intercept SOAP messages between Web services clients and servers so that you can inspect, and if you like, alter the results. In Step-by-Step 4.9 you use one of these tools to see a Web service in action.

STEP BY STEP

4.9 Testing a Web Service without a Client Project

  1. Download the .NET WebService Studio tool from www.gotdotnet.com/team/tools/web_svc/default.aspx and install it on your computer.

  2. Launch the WebServiceStudio.exe application.

  3. Enter http://live.capescience.com/wsdl/AirportWeather.wsdl as the WSDL endpoint and click the Get button.

  4. The tool reads the WSDL file from the Web service and constructs the necessary proxy classes to invoke it. Click the getSummary entry on the Invoke tab to use the getSummary Web method.

  5. In the Input section, select the arg0 item. You can now enter a value for this item in the Value section. Enter an airport code such as KSEA for the value.

  6. Click the Invoke button. The tool sends a SOAP message to the Web service, using your chosen parameters, and then displays the results, as shown in Figure.

    14. The .NET WebService Studio tool allows you to test a Web service without creating any client application.

    graphics/04fig14.jpg

  7. Click the Request/Response tab to view the outgoing and incoming SOAP messages.

  8. Click the WSDLs & Proxy tab to see the WSDL file and the generated proxy class for this Web service.

REVIEW BREAK

  • You can manually generate proxy classes for a Web service by using the Web Services Description Language tool.

  • You can automatically generate proxy classes for a Web service by setting a Web reference to point to the Web service.

  • You can test and debug a Web service without a client application by using one of several SOAP proxy tools.