In the first part of this article, I discussed the option of calling multiple web services and waiting for them to all return.

It turns out you can do it quickly with a method call that already existed:


public List GetProducts( int[] productCodes )  
{
     ArrayList asyncStore = new ArrayList();
     List<int, ProductWebService.Product> products = new List<int, ProductWebService.Product>(); 
     ProductWebService.Products service = new ProductWebService.Products();
 
     service.GetProductDataCompleted += new GetProductDataCompletedEventHandler(GetProductData_Completed);
     GetProductDataDelegate asyncCall = new GetProductDataDelegate(service.GetProductData);
 
     foreach(int productCode in productCodes) 
     { 
          IAsyncResult result = asyncCall.BeginInvoke(productCode,null,productCode);
          asyncStore.Add(result.AsyncWaitHandle);
     }
 
     WaitHandle[] waitHandles = (WaitHandle[])asyncStore.ToArray(typeof(WaitHandle[]));
     WaitHandle.WaitAll ( waitHandles );
 
     //Once here, we can guarantee all of the web services have fired!
} 
 
protected void GetProductData_Completed(object sender, GetProductDataEventArgs e) 
{ 
    //Single web service fired
}

 

Therefore, you can use WaitAll() for your web services!


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
posted @ Tuesday, June 17, 2008 8:58 AM | in Web Services and .NET Remoting

Comments

Gravatar
# re: Using delegates to simulate WaitHandle.WaitAll() calls for web service - Part 2
Posted by ecogtip
on 6/19/2008 10:19 AM
dominic,

im not sure what i'm missing but the code below had some syntax problem(aside from the missing end parenthesis):

service.GetProductDataCompleted += new GetProductDataCompletedEventHandler(

GetProductDataDelegate asyncCall = new GetProductDataDelegate(service.GetProductData);

here is how i am using it in my code:

service.GetTicketsCompleted +=new GetTicketsCompletedEventHandler(
CompleteGetTicketData cgtd = new CompleteGetTicketData(service.GetTickets));

CompleteGetTicketData is a delegate declared as:
public delegate void CompleteGetTicketData(object sender, System.ComponentModel.AsyncCompletedEventArgs e);

compiler doesn't like the statement. do you have a code that actually compiles and run.

thanks
Gravatar
# re: Using delegates to simulate WaitHandle.WaitAll() calls for web service - Part 2
on 6/19/2008 10:48 AM
Apologies,

It seems by recently posted part 2 post was unfinished. I have placed the method I wished to call into the example now.

Here is the code:

service.GetProductDataCompleted += new GetProductDataCompletedEventHandler(GetProductData_Completed);

The method you wish to call on the event is placed inside the parenthesis. This example would notify me when a web service call has completed, call the GetProductData_Completed() method.

Hope this helps,

Dominic
Gravatar
# re: Using delegates to simulate WaitHandle.WaitAll() calls for web service - Part 2
Posted by ecogtip
on 6/19/2008 11:24 AM
ey man,

how about this GetProductDataDelegate where is this defined?

also the proxy class i generated for the web service has a method im using that is postfix Async (e.g. GetTicketsAsync) do you have an idea what is this for.

more power.

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 1 and 1 and type the answer here: