2

I have an ASP.NET page that needs to make calls to multiple web services to return data. I'd like to run these requests in parallel, and using the PageAsyncTask class appears to be the most appropriate way to do that. The trick is that there are five calls--say A, B, C, D, and E. Calls A & B must be made sequentially, as must C & D. The AB pair, the CD pair, and E can all run in parallel.

It doesn't appear that I can use the "executeInParallel" parameter of the PageAsyncTask constructor to create 5 tasks and get this configuration of parallel/sequential.

I've tried creating 3 tasks, two of which have chained asynchronous calls (similar to http://msdn.microsoft.com/en-us/library/dwba7yy7(VS.80).aspx). When I try to do that in the context of a PageAsyncTask, I get an error indicating that the end function can only be called once. I've tried wrapping the chained set of 3 functions in a pair of functions that the PageAsyncTask uses, and that results in the end function never being called (thus a timeout).

Is there a good way to do this?

Edit: If it's useful, here's a simplified version of the code I'm attempting and getting the error that the end function can only be called 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
joelt
  • 2,672
  • 2
  • 24
  • 32
  • I think instead of pursuing this, you should look at why the client needs to make 5 requests. Why can't this be done in a single round-trip? It would be a much better experience for the client, especially ones with high latency. – Nick Craver May 17 '10 at 15:05
  • Part of the reason is that the AB, CD, and E calls are to three different web services. In some cases, the AB pair is to a web service I have no control over, and it's designed such that call A authenticates, and call B returns data. In other cases, I've tested and found that splitting into two calls is more performant. – joelt May 17 '10 at 15:24

1 Answers1

1

If anyone runs across the same issue, I was able to make this work by going back to wrapping my chain of calls in a pair of async calls, but then implementing my own IAsyncResult as outlined here: http://msdn.microsoft.com/en-us/magazine/cc163467.aspx

Then I decided the benefits didn't justify any potential maintenance/debugging costs.

joelt
  • 2,672
  • 2
  • 24
  • 32