views:

16

answers:

1

I have a WCF service that I want to use the async pattern on as it mainly calls a series of web services and then processes the results once all of the web service calls have returned.

However, the async pattern in WCF only supports a single IAsyncResult being returned in the BeginXXX method.

I was wondering if anyone had any experience of creating a composite IAsyncResult that could signal once all the web services have returned, or have any other ideas to handle the problem?

A: 

I think basically you'll need to create your own custom IAsyncResult implementation as well as doing some serious book-keeping around your requests. I'm assuming you'll want to fire your internal requests in parallel and then wait until all are done before returning, right?

If so, then what I'd do is create a custom IAsyncResult, while internally keeping track of each of the IAsyncResults associated with each internal call, then only consider my custom IAsyncResult done (which implies both signaling it's wait handle, as well as firing any callbacks) when all the internal requests are done.

tomasr
Thanks for the feedback, that's what I was thinking of doing, I was hoping that there would be a reference someone would be able to point me to.In terms of the approach I need to take then yes you are right - it's basically calling a few different services in parallel (e.g. do a vehicle lookup, do a postcode lookup) in different locations.When you say book-keeping, does that mean you have concerns about the approach in general?
spooner
No concerns, it should work; just that you have to be careful implementing it as it's easy to screw up :).What I meant with book keeping is that you've got to keep track of the state of each call and make sure you handle errors, etc from each one, which usually means, among others, deciding what you want to do if one out of the N service calls fails (i.e. do you return an error or a partial response, etc).Implementing IAsyncResult isn't actually too hard, there's a sample one here: http://blogs.msdn.com/b/ploeh/archive/2007/02/09/agenericiasyncresultimplementation.aspx
tomasr
Ah ok. I agree it doesn't look too bad, but as you say easy to screw up :)Do you think this pattern would also be appropriate if we had to call several long running procedures on a database (e.g. 2 secs each)? The reason for that is that some of our legacy functionality has lots of business logic in the database so it takes a while to execute there, and it seems a waste of web server resources to run them synchronously (can be upto 6 calls). Obviously this adds more complexity to the code, but it seems that it would be worth it to improve the scalability of the web servers.
spooner
It depends on whether you're starving the IIS/WCF thread pools, but yeah, if you've got lots of other concurrent calls going on, you could potentially get some improved behavior if you get the processing off the IIS/WCF threads into your own stuff.
tomasr