ASP.NET Asynchronous Calls
I have an ASP.NET page that needs to make calls to multiple web services to return data. I would like to run these requests in parallel, and using the PageAsyncTask class seems to be the most appropriate way to do this. The trick is that there are five calls — say A, B, C, D, and E. Calls A and B must be made in sequence, just like C and D. Pair AB, pair CD and E can work in parallel.
It doesn't look like I can use the "executeInParallel" parameter of the PageAsyncTask constructor to create 5 tasks and get this parallel / sequential configuration.
I tried to create 3 tasks, two of which have a chain of asynchronous calls (similar to http://msdn.microsoft.com/en-us/library/dwba7yy7(VS.80).aspx ). When I try to do this in the PageAsyncTask context, I get an error indicating that the final function can only be called once. I tried wrapping a chain of 3 functions in a couple of functions that the PageAsyncTask page uses and this results in the final function never being called (thus time-out).
Is there a good way to do this?
Edit: If it's helpful, here's a simplified version of the code I'm trying to get and that is an error that a final function can only throw once.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objMB As New BusObject.AsyncTasks(AddressOf dummy)
Dim objTask As New PageAsyncTask(AddressOf objMB.BeginStartSession, AddressOf objMB.EndGetList, Nothing, Nothing, True)
RegisterAsyncTask(objTask)
Dim i As Integer = 0
End Sub
Public Class BusObject
Public Class AsyncState
Public SecondCallback As AsyncCallback
End Class
Public Class AsyncTasks
Dim fn_Callback As PageCallback
Public Delegate Sub PageCallback(ByVal objData As Object)
Public Sub New(ByVal fnCallback As PageCallback)
fn_Callback = fnCallback
End Sub
Public Function BeginStartSession(ByVal sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal extraData As Object) As IAsyncResult
Dim objWS As New com.WebService
Dim objX As New AsyncState
objX.SecondCallback = cb
Return objWS.BeginWSFunction1("param1", "param2", AddressOf EndStartSession, objX)
End Function
Public Sub EndStartSession(ByVal objAsyncResult As IAsyncResult)
Dim objWS As New com.WebService
Dim strSessionKey As String = objWS.EndWSFunction1(objAsyncResult)
Dim objState As AsyncState = objAsyncResult.AsyncState
objWS.BeginWSFunction2("p1", "p2", "p3", "p4", "p5", strSessionKey, True, objState.SecondCallback, objState)
End Sub
Public Sub EndGetList(ByVal objState As IAsyncResult)
Dim objWS As New com.WebService
Dim objResult As com.WebService.Result = objWS.EndWSFunction2(objState)
Dim lstReturn As New List(Of BusObject)
fn_Callback(lstReturn)
End Sub
End Class
End Class
a source to share
If anyone is facing the same problem, I was able to get the job done by reverting to completing my call chain in a couple of asynchronous calls, but then doing my own IAsyncResult as described here: http://msdn.microsoft.com/ en-us / magazine / cc163467.aspx
I then decided that the benefits did not justify any potential maintenance / debugging costs.
a source to share