Introduction:
In Windows Communication Foundation (WCF)
applications, a service operation can be implemented asynchronously or
synchronously. For example, asynchronous service operations can be calling
synchronously, and synchronous service operations can be called asynchronously.
Asynchronous operations allow a client to send a
request to the server and the server callback to the client when the
operation is complete.In this way, the client can free himself to do other things
and gets around any message timeout issues.
In WCF
applications, asynchronous requests in two
ways:
- Event Based Model: Event based asynchronous operations. This occurs on the client-side only.
- IAsyncResult Model:Asynchronous operations using System.IAsyncResult based object. This can occur on both the client-side or server-side.
Need of asynchronous service operations:
Implementing asynchronous services is required
when server side having workload that means server side performs heavy
operations which might block the call for a long duration, such as I/O work,
approaching databases etc.In all other cases, implement the service
synchronously (which will cause the operation to block during the execution of
the operation).
An important thing that need to know that there
is no dependency between the way the client is implemented and the way the
server is. I mean to say, the client can implement the contract synchronously
while it is implemented asynchronously on the server side and vice versa.
Asynchronous operations in WCF
Series:
-
Event Based Model
-
IAsyncResult Model (client-side)
-
IAsyncResult Model (server-side)
-
Canceling Operations
-
Handling Exceptions
Implementing the Service
operation asychronously:
- In your service contract, declare an asynchronous method pair according to the .NET asynchronous design guidelines. The Begin method takes a parameter, a callback object, and a state object, and returns a System.IAsyncResult and a matching End method that takes a System.IAsyncResult and returns the return value. For more information about asynchronous calls.
- Mark the Begin method of the asynchronous method pair with the System.ServiceModel.OperationContractAttribute attribute and set the System.ServiceModel.OperationContractAttribute attribute.AsycnPattern property to true. For example, the following code performs steps 1 and 2.
Step 1: Open Visual Studio and go to the
File menu.
- Select WCF Service Application in Visual Basic.
- The IService.vb will be open.
Code:
' NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
<ServiceContract()> _
Public Interface IService1
<OperationContract()> _
Function samplemethod(msgAs String)As String
<OperationContract(AsyncPattern:=True)> _
Function Beginsamplemethod(msgAs String, cb As AsyncCallback, state As Object) As IAsyncResult
Function Endsamplemethod(rAs IAsyncResult)As String
' TODO: Add your service operations here
End Interface
Step 2: Open 'Service.svc.vb' and write the code:
Code:
' NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
Public Class Service1
Implements IService1
Public Function samplemethod(msg As String) As String
Console.WriteLine("Called synchronous sample method with ""{0}""", msg)
Return "The sychronous service greets you: " & msg
End Function
' This asynchronously implemented operation is never called because
' there is a synchronous version of the same method.
Public Function Beginsamplemethod(msgAs String, callback As AsyncCallback, StateAs Object) As IAsyncResult
Console.WriteLine("BeginSampleMethod called with: " & msg)
Return New CompletedAsyncResult(Of String)(msg)
End Function
Public Function Endsamplemethod(r As IAsyncResult)As String
Dim result As CompletedAsyncResult(Of String) = TryCast(r, CompletedAsyncResult(Of String))
Console.WriteLine("EndSampleMethod called with: " + result.Data)
Return result.Data
End Function
End Class
Step 3: Add a class by Right clicking on the project in solution explorer.
- Give the name of the class 'CompletedAsyncResult'.
Public Class CompletedAsyncResult(Of T)
Implements IAsyncResult
Private m_data As T
Public Sub New(data As T)
Me.m_data = data
End Sub
Public ReadOnly Property Data()As T
Get
Return m_data
End Get
End Property
#Region "IAsyncResult Members"
Public ReadOnly Property AsyncState() As Object
Get
Return DirectCast(m_data,Object)
End Get
End Property
Public ReadOnly Property AsyncWaitHandle()As WaitHandle
Get
Throw New Exception("The method or operation is not implemented.")
End Get
End Property
Public ReadOnly Property CompletedSynchronously() As Boolean
Get
Return True
End Get
End Property
Public ReadOnly Property IsCompleted()As Boolean