Team LiB
Previous Section Next Section

Review of Web Service Consumption

This next section will provide you with a quick review of consuming web services. In the samples that follow, the code consumes the service created earlier using both synchronous and asynchronous method calls. Much of this information can be seen in the section of this book that discusses Windows Forms and how to consume web services from a Windows Forms application.

Creating a Client Proxy for a Web Service

The first step toward consuming a web service is the creation of a client proxy. You can either let Visual Studio .NET 2003 create the proxy for you by adding a web reference or you can use the WSDL.EXE command-line tool to create the proxy class.

Whether you use Visual Studio or the command-line to create the proxy, the proxy will be identical. It will contain method definitions for both synchronous and asynchronous calls. When you invoke the methods on the proxy, they will be forwarded over the network in the appropriate wire format to the web service.

See Chapter 41 or another reference on web services for details on using WSDL.EXE to create client proxies. The next two samples will both be written using Visual Studio .NET 2003 and web references.

Making Synchronous Calls

Making synchronous calls is the easiest way to communicate with web services. The code in Listing 32.4 shows a very simple synchronous execution in response to a button press. It is made possible by creating a new Windows Forms application, adding a web reference to the web service created earlier (HelloService), and placing some input controls and some buttons on a form.

Listing 32.4. The Code Supporting Synchronous Web Service Calls
private void button1_Click(object sender, System.EventArgs e)
{
  HelloService.HelloService proxy = new HelloService.HelloService();
  MessageBox.Show(this, proxy.HelloWithParameters(
    dtStartTime.Value, Int32.Parse(txtDaycount.Text), txtUserName.Text));
}

Making Asynchronous Calls

Making asynchronous calls is a little trickier because the processing of the call to the web service takes place on a background thread. The result of this is that you can make calls to long-running web service tasks without blocking the user interface so that the user doesn't think the application is hung or broken.

The code from Listing 32.5 shows the methods necessary to asynchronously invoke the web service and display the results. The Invoke method is used so that the foreground thread is asked to perform the operation. As was just mentioned, asynchronous web service processing is done on the background thread. Some tasks (such as opening new forms) don't happen unless they're performed by the foreground thread.

Listing 32.5. Methods Supporting Asynchronous Execution of a Web Service Call
private void button2_Click(object sender, System.EventArgs e)
{
  statusBar1.Panels[0].Text = "Invoking HelloWithParameters Asynchronously....";
  HelloService.HelloService proxy = new HelloService.HelloService();
  proxy.BeginHelloWithParameters(
    dtStartTime.Value,
    Int32.Parse(txtDaycount.Text), txtUserName.Text,
    new AsyncCallback(FinishHelloWithParameters), null);
}

private void FinishHelloWithParameters(IAsyncResult ar )
{
  HelloService.HelloService proxy = new HelloService.HelloService();
  string result = proxy.EndHelloWithParameters( ar );
  statusBar1.Panels[0].Text = "Asynchronous Invocation Completed.";
  this.Invoke( new ShowMessageDelegate(ShowMessage), new object[] { result } );
}

private void ShowMessage( string message )
{
  MessageBox.Show(this, message );
}

When a user clicks the button to invoke the code asynchronously, the BeginHelloWithParameters method is called with a new asynchronous callback delegate. When the method on the server finishes its work and returns, the callback delegate is invoked by the framework and the method can then extract the results. As mentioned earlier, the Invoke method is used to forward the request to display a message box to the foreground thread of the user interface.

    Team LiB
    Previous Section Next Section