views:

92

answers:

2

I have MVC application (applies to non MVC as well) where a user is posting in data. I need to take this data, send it off to two seperate end points (one using a WebRequest form POST and one using a Web Service), parse the result, and send the result back to the original user.

The issue at hand is that both end points take about 20-30 seconds to respond (response is a string) which means that I should probably execute these two calls asynchronously. At the same time I want to wait to respond to the original user until I get both results back. I am guessing I might have to use some sort of object lock so the response does not get sent back before the two calls are complete?

Am I on the right path? Does anyone have any information on how to achieve this? Any help is appreciated.

Thanks

EDIT

Based on the responses I decided to go with async controllers since I am already working with a MVC application. Thank you for your input.

+1  A: 

You can invoke Join on the two asynchronous threads to wait for their return. You'll also want to look into asynchronous controllers. This is available in MVC2 but you can also look at the MVC1 features I believe to implement asynchronous actions. You'll want to do this so you're not blocking IIS from processing more threads.

andymeadows
A: 

I think you'll find this helpful: Rx: Piecing together multiple IObservable web requests

In particular, using ForkJoin to wait until both responses come back as mentioned in the comments:

Also bonus question, there is a case where I would like to execute web calls simultaneously and when all are finished execute another observable which will until other calls are finished.

Use Observable.ForkJoin to execute multiple async calls simultaneously and then join all of the results into a single IObservable. Then use SelectMany (another from statement) just like above, to subscribe to another observable based on the joined result.

Richard Hein