Calling the Service Asynchronously


When you send a message across the network, you always have to be aware of the network latency. If the Web service is invoked synchronously, the client application is blocked until the call returns. This may be fast enough in a local network; however, you must pay attention to the production system's network infrastructure.

You can send messages to the Web service asynchronously. The client proxy creates not only synchronous methods but also asynchronous methods. However, there's a special issue with Windows applications. Because every Windows control is bound to a single thread, methods, and properties of Windows controls may only be called from within the creation thread. The proxy class of .NET 2.0 has some special features for this issue as you can see with the generated proxy code.

Here you can see the asynchronous version of the method ReverseString(). With the asynchronous implementation of the proxy class, there's always a method that can be invoked asynchronously, and an event where you can define what method should be invoked when the Web service method is finished. Let's get into this with more detail. The method ReverseStringAsync() only has the parameters that are sent to the server. The data that are received from the client can be read by assigning an event handler to the event ReverseStringCompleted. This event is of type ReverseStringCompletedEventHandler. ReverseStringCompletedEventHandler is a delegate where the second parameter (ReverseStringCompletedEventArgs) is created from the input parameters of the ReverseString() method. The class ReverseStringCompletedEventArgs contains the return data from the Web service in the Result property. Why this implementation works is with the SendOrPostCallback delegate. This delegate forwards the call to the correct thread of the Windows Forms control.

 public event ReverseStringCompletedEventHandler ReverseStringCompleted; public void ReverseStringAsync(string message) { this.ReverseStringAsync(message, null); } public void ReverseStringAsync(string message, object userState) { if ((this.ReverseStringOperationCompleted == null)) { this.ReverseStringOperationCompleted =  new System.Threading.SendOrPostCallback( this.OnReverseStringOperationCompleted); } this.InvokeAsync("ReverseString", new object[] { message), this.ReverseStringOperationCompleted, userState); } public void OnReverseStringOperationCompleted(object arg) { if ((this.ReverseStringCompleted != null)) { System.Web.Services.Protocols.InvokeCompledtedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg)); this.ReverseStringCompleted(this,  new ReverseStringCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState); } } public delegate void ReverseStringCompletedEventHandler(object sender,  ReverseStringCompletedEventArgs e); public partial class ReverseStringCompletedEvenArgs :  System.ComponentModel.AsyncCompletedEventArgs { private object[] results; internal ReverseStringCompletedEventArgs(object[] results,  System.Exception exception, bool cancelled, object userState) : base(exception, cancelled, userState) { this.results = results; } public string Result { get { this.RaiseExceptionIfNecessary(); return ((string)(ths.results[0])); } } } 

Now let's use the asynchronous implementation of the proxy class.

  1. Create a handler method with the name WebServicesResult that has the same signature as is defined by the delegate ReverseStringCompletedEventHandler. This method will be invoked when the Web service call is completed. With the implementation the Result property of the ReverseStringComletedEventArgs parameter is passed to the Text property of textBox2:

     private void WebServicesResult(object sender,  WebServicesSample.ReverseStringCompletedEventArgs e) { textBox2.Text = e.Result; } 
  2. To invoke the Web service asynchronously, change the implementation of the method button1_ Click. After the proxy is instantiated, add the previous created method WebServicesResult to the event ReverseStringCompleted. Next you can invoke the asynchronous method of the proxy ReverseStringAsync, and pass the Text property from textBox1. With the asnyc method a thread is created that does the call to the Web service.

    private void button1_Click(object sender, EventArgs e) {    // asynchronous version    WebServicesSample.Service ws = new WebServicesSample.Service(); ws.ReverseStringCompleted += WebServicesresult; ws.ReverseStringAsync(textBox1.Text); }

Now you can run the client once more. You can also add a sleep interval to the Web service implementation, so you can see that the UI of the client application is not stalled while the Web service is invoked.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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