如何在VB.NET中调用这个方法?
How to call this method in VB.NET?
我正在制作一个使用 SOAP 网络服务的 VB.NET 应用程序。
它在使用 运行 方法的同步模式下工作得很好。
但我想使用这种异步方法:
Public Overloads Sub runAsync(ByVal callContext As CAdxCallContext, ByVal publicName As String, ByVal inputXml As String, ByVal userState As Object)
If (Me.runOperationCompleted Is Nothing) Then
Me.runOperationCompleted = AddressOf Me.OnrunOperationCompleted
End If
Me.InvokeAsync("run", New Object() {callContext, publicName, inputXml}, Me.runOperationCompleted, userState)
End Sub
怎么称呼它?
回调方法如何表示?
谢谢
彼得
TaskCompletionSource
将是用异步方法包装基于事件的方法的正确工具。
创建接受 TaskCompletionSource
实例作为参数的完成事件处理程序。
Public Sub OperationCompleted(
e As SomeEventArgs,
TaskCompletionSource(Of SoapResult) taskSource
)
' Use EventArgs of your client to determine were operation successful or not
' Here I am using common completion properties
If e.Cancelled Then
taskSource.TrySetCanceled()
Exit Sub
End If
If e.HasError Then
taskSource.TrySetException(e.Error)
Exit Sub
End If
' Should be success now
taskSource.TrySetResult(e.Result) ' Result is an instance of type SoapResult
End Sub
异步方法returns任务,在本例中我们期望操作成功时返回SoapResult
类型的实例
Public Function RunAsync(MyInputArguments args) As Task(Of SoapResult)
Dim taskSource As New TaskCompletionSource(Of SoapResult)()
Dim invokeArgs As New Object() { args.Context, args.PublicName, args.InputXml }
' Wrap our completion handler with the function
' To satisfy soap callback signature
Dim callback As Action(Of SomeEventArgs) =
Sub(e) Me.OperationCompleted(e, taskSource)
InvokeAsync("run", invokeArgs, callback, args.UserState)
Return taskSource.Task
End Function
现在我们可以使用 async-await
异步调用 Soap 客户端
Dim arguments As New With
{
.Context = callContext,
.PublicName = publicName,
.InputXml = inputXml,
.UserState = userState
}
Dim result As SoapResult = Await client.RunAsync(arguments)
感谢您的帮助,它让我走上了正轨。
我找到了如何使用异步方法调用和获取回调:
致电:
x3WebService.runAsync(callContext, webserv, params, UserState)
回调:
Public Sub RunAsyncCompleted(sender As Object, e As runCompletedEventArgs) Handles x3WebService.runCompleted
...
但是你必须声明“withevents” x3WebService class :
Public Class X3WebServices
Private WithEvents x3WebService As CAdxWebServiceXmlCCServiceBasicAuth
你好,
彼得
我正在制作一个使用 SOAP 网络服务的 VB.NET 应用程序。 它在使用 运行 方法的同步模式下工作得很好。 但我想使用这种异步方法:
Public Overloads Sub runAsync(ByVal callContext As CAdxCallContext, ByVal publicName As String, ByVal inputXml As String, ByVal userState As Object)
If (Me.runOperationCompleted Is Nothing) Then
Me.runOperationCompleted = AddressOf Me.OnrunOperationCompleted
End If
Me.InvokeAsync("run", New Object() {callContext, publicName, inputXml}, Me.runOperationCompleted, userState)
End Sub
怎么称呼它?
回调方法如何表示?
谢谢
彼得
TaskCompletionSource
将是用异步方法包装基于事件的方法的正确工具。
创建接受 TaskCompletionSource
实例作为参数的完成事件处理程序。
Public Sub OperationCompleted(
e As SomeEventArgs,
TaskCompletionSource(Of SoapResult) taskSource
)
' Use EventArgs of your client to determine were operation successful or not
' Here I am using common completion properties
If e.Cancelled Then
taskSource.TrySetCanceled()
Exit Sub
End If
If e.HasError Then
taskSource.TrySetException(e.Error)
Exit Sub
End If
' Should be success now
taskSource.TrySetResult(e.Result) ' Result is an instance of type SoapResult
End Sub
异步方法returns任务,在本例中我们期望操作成功时返回SoapResult
类型的实例
Public Function RunAsync(MyInputArguments args) As Task(Of SoapResult)
Dim taskSource As New TaskCompletionSource(Of SoapResult)()
Dim invokeArgs As New Object() { args.Context, args.PublicName, args.InputXml }
' Wrap our completion handler with the function
' To satisfy soap callback signature
Dim callback As Action(Of SomeEventArgs) =
Sub(e) Me.OperationCompleted(e, taskSource)
InvokeAsync("run", invokeArgs, callback, args.UserState)
Return taskSource.Task
End Function
现在我们可以使用 async-await
Dim arguments As New With
{
.Context = callContext,
.PublicName = publicName,
.InputXml = inputXml,
.UserState = userState
}
Dim result As SoapResult = Await client.RunAsync(arguments)
感谢您的帮助,它让我走上了正轨。
我找到了如何使用异步方法调用和获取回调:
致电:
x3WebService.runAsync(callContext, webserv, params, UserState)
回调:
Public Sub RunAsyncCompleted(sender As Object, e As runCompletedEventArgs) Handles x3WebService.runCompleted
...
但是你必须声明“withevents” x3WebService class :
Public Class X3WebServices
Private WithEvents x3WebService As CAdxWebServiceXmlCCServiceBasicAuth
你好,
彼得