ASP.NET 链式异步调用
我有一个 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人遇到同样的问题,我可以通过返回将调用链包装在一对异步调用中来完成这项工作,然后实现我自己的 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.