This is a mirror of official site: http://jasper-net.blogspot.com/

How to: Implement a WCF Asynchronous Service Operation with Task

| Wednesday, May 16, 2012
I love Task<T>.  It has to be one of the finest innovations in the framework in the past few years.  Recently I was reviewing some old WCF documentation How to: Implement an Asynchronous Service Operation which was probably written in 2006 and I have to say that we can do a much better job of it with Task<T> so here goes my rewrite.

Download Sample Code How to: Implement and Consume a WCF Asynchronous Service Operation with Task<T>

Implement a service operation asynchronously
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, see Asynchronous Programming Design Patterns.

Mark the Begin method of the asynchronous method pair with the System.ServiceModel.OperationContractAttribute attribute and set the System.ServiceModel.OperationContractAttribute.AsyncPattern property to true. For example, the following code performs steps 1 and 2.

[ServiceContract]
public interface ISampleTaskAsync
{
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginDoWork(int count, AsyncCallback callback, object state);

int EndDoWork(IAsyncResult result);
}

Implement the Begin/End method pair in your service class according to the asynchronous design guidelines. For example, the following code example shows an implementation in which a string is written to the console in both the Begin and End portions of the asynchronous service operation, and the return value of the End operation is returned to the client. For the complete code example, see the Example section.

Example
The following code examples show:

Read more: Ron Jacobs
QR: Inline image 1

Posted via email from Jasper-net

0 comments: