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

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.

Published Sunday, March 13, 2011 10:07 PM by javarg

Comments

# Async C# 5: Using Async Pattern with WCF and Silverlight

Tuesday, April 12, 2011 10:30 PM by Javier Arguello (javarg)

Sometime ago I published a way to use WCF service with the new Async Pattern in C#. The problem is that

# Async C# 5: Using Async Pattern with WCF and Silverlight

Wednesday, April 13, 2011 2:21 AM by Async C# 5: Using Async Pattern with WCF and Silverlight

Pingback from  Async C# 5: Using Async Pattern with WCF and Silverlight

Leave a Comment

(required) 
(required) 
(optional)
(required)