To call WCF service operations asynchronously
Run the ServiceModel Metadata Utility Tool (Svcutil.exe) tool with both the /async and the /tcv:Version35 command options together as shown in the following command.
Code:
svcutil /n:http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples http://localhost:8000/servicemodelsamples/service/mex /a /tcv:Version35
This generates, in addition to the synchronous and standard delegate-based asynchronous operations, a WCF client class that contains:
- Two <operationName>Async operations for use with the event-based asynchronous calling approach. For example:
Code:
Public Overloads Sub AddAsync(ByVal n1 As Double, ByVal n2 As Double)
Me.AddAsync(n1, n2, Nothing)
End Sub
Public Overloads Sub AddAsync(ByVal n1 As Double, ByVal n2 As Double, ByVal userState As Object)
If (Me.onBeginAddDelegate Is Nothing) Then
Me.onBeginAddDelegate = AddressOf Me.OnBeginAdd
End If
If (Me.onEndAddDelegate Is Nothing) Then
Me.onEndAddDelegate = AddressOf Me.OnEndAdd
End If
If (Me.onAddCompletedDelegate Is Nothing) Then
Me.onAddCompletedDelegate = AddressOf Me.OnAddCompleted
End If
MyBase.InvokeAsync(Me.onBeginAddDelegate, New Object() {n1, n2}, Me.onEndAddDelegate, Me.onAddCompletedDelegate, userState)
End Sub
- Operation completed events of the form <operationName>Completed for use with the event-based asynchronous calling approach. For example:
Code:
Public Event AddCompleted As System.EventHandler(Of AddCompletedEventArgs)
- System.EventArgs types for each operation (of the form <operationName>CompletedEventArgs) for use with the event-based asynchronous calling approach. For example:
Code:
<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")> _
Partial Public Class AddCompletedEventArgs
Inherits System.ComponentModel.AsyncCompletedEventArgs
Private results() As Object
Public Sub New(ByVal results() As Object, ByVal exception As System.Exception, ByVal cancelled As Boolean, ByVal userState As Object)
MyBase.New(exception, cancelled, userState)
Me.results = results
End Sub
Public ReadOnly Property Result() As Double
Get
MyBase.RaiseExceptionIfNecessary
Return CType(Me.results(0),Double)
End Get
End Property
End Class
Bookmarks