Async CTP (C# 5): How to make WCF work with Async CTP

Posted by javarg on ASP.net Weblogs See other posts from ASP.net Weblogs or by javarg
Published on Mon, 14 Mar 2011 03:07:26 GMT Indexed on 2011/03/14 8:10 UTC
Read the original article Hit count: 687

Filed under:
|
|
|

If you have recently downloaded the new Async CTP you will notice that WCF uses Async Pattern and Event based Async Pattern in order to expose asynchronous operations.

In order to make your service compatible with the new Async/Await Pattern try using an extension method similar to the following:

WCF Async/Await Method
public static class ServiceExtensions
{
    public static Task<DateTime> GetDateTimeTaskAsync(this Service1Client client)
    {
        return Task.Factory.FromAsync<DateTime>(
            client.BeginGetDateTime(null, null),
            ar => client.EndGetDateTime(ar));
    }
}

The previous code snippet adds an extension method to the GetDateTime method of the Service1Client WCF proxy.

Then used it like this (remember to add the extension method’s namespace into scope in order to use it):

Code Snippet
var client = new Service1Client();
var dt = await client.GetDateTimeTaskAsync();

Replace the proxy’s type and operation name for the one you want to await.

© ASP.net Weblogs or respective owner

Related posts about c#

Related posts about async