ASP.NET 链式异步调用

发布于 2024-09-01 11:49:34 字数 2507 浏览 1 评论 0 原文

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

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

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

有没有好的方法可以做到这一点?

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

     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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-09-08 11:49:34

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

然后我决定好处并不能证明任何潜在的维护/调试成本是合理的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文