ASP.NET链接异步调用

时间:2010-05-17 15:01:19

标签: asp.net asynchronous pageasynctask

我有一个ASP.NET页面需要调用多个Web服务来返回数据。我想并行运行这些请求,并且使用PageAsyncTask类似乎是最合适的方法。诀窍是有五个电话 - 比如A,B,C,D和E.呼叫A& B必须按顺序制作,C& C必须按顺序制作。 D. AB对,CD对和E都可以并行运行。

似乎我不能使用PageAsyncTask构造函数的“executeInParallel”参数创建5个任务并获得并行/顺序的配置。

我尝试过创建3个任务,其中两个已经链接了异步调用(类似于http://msdn.microsoft.com/en-us/library/dwba7yy7(VS.80).aspx)。当我尝试在PageAsyncTask的上下文中执行此操作时,我收到一个错误,指示只能调用一次end函数。我已经尝试在PageAsyncTask使用的一对函数中包装链接的3个函数集,这导致函数永远不被调用(因此超时)。

有没有好办法呢?

编辑:如果它有用,这里是我正在尝试的代码的简化版本,并且得到的错误是只能调用一次结束函数。

     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

1 个答案:

答案 0 :(得分:1)

如果有人遇到同样的问题,我可以通过返回在一对异步调用中包装我的调用链来完成这项工作,然后按照此处所述实现我自己的IAsyncResult:http://msdn.microsoft.com/en-us/magazine/cc163467.aspx < / p>

然后我认为这些好处并不能证明任何潜在的维护/调试成本。