17.9 Calling Web Methods Asynchronously

 <  Day Day Up  >  

17.9 Calling Web Methods Asynchronously

You want to call a method implemented in a Web Service asynchronously.


Technique

In addition to containing methods for each method defined in a Web Service, the generated proxy class also contains the methods needed to call Web Service methods asynchronously. Each method within the proxy class has an additional two methods used for asynchronous invocation, corresponding to the beginning of the asynchronous call and the asynchronous method call results.

The following example uses the GenerateNumbers method defined in the Lottery Web Service created earlier in this chapter. To begin the method call, call the Begin version of the method, which for the lottery service is called BeginGenerateNumbers . All the parameters are the same as those in the GenerateNumbers method except for two additional parameters specifying the callback method and an object to use for the final result. The callback method is called when the Web Service method returns. The return type is void , and the parameter is an object implementing the IAsyncResult interface. Within the callback method, call the EndGenerateNumbers method, passing the IAsyncResult object to obtain the results.

Listing 17.7 Making Asynchronous Web Method Calls
 private void btnGo_Click(object sender, System.EventArgs e) {     localhost.LotteryNumberGenerator gen = new         localhost.LotteryNumberGenerator();     gen.BeginGenerateNumbers( 20000, 0, 100000, true,         new AsyncCallback(GenerateNumbersCallback), gen );     startTime = DateTime.Now;     pbWait.Value = 0;     timer1.Enabled = true; } public void GenerateNumbersCallback(IAsyncResult ar) {     DateTime endTime = DateTime.Now;     // get result     localhost.LotteryNumberGenerator gen =         (localhost.LotteryNumberGenerator) ar.AsyncState;     string result = gen.EndGenerateNumbers( ar );     // stop timer and computer total time     timer1.Enabled = false;     TimeSpan span = new TimeSpan( endTime.Ticks - startTime.Ticks );     MessageBox.Show( "Call time = "+span.ToString()+"\nResult: "+result ); } private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {     pbWait.PerformStep();     if( pbWait.Value >= pbWait.Maximum )         pbWait.Value = 0; } 

Comments

If you read Chapter 15, "Sockets," then this process of making an asynchronous call should seem familiar. In Chapter 15, a series of asynchronous calls were made to request a Web document. The proxy class acts as a dual proxy of sorts. When you call one of the asynchronous methods, the actual call doesn't occur within the proxy class but rather a generic base class asynchronous method named BeginInvoke . The same method of passing the actual method invocation to SoapHttpClientProtocol also occurs when calling a method synchronously in that the actual method call is through the Invoke method defined in the base class of the proxy class.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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