It's been a while since I've worked on WP7, so I'm going to assume that the client proxy has both EAP endpoints (*Async
+ *Completed
) and APM endpoints (Begin*
+ End*
) for each contract method.
If that's correct, then you can use TaskFactory.FromAsync
to wrap the Begin*
/ End*
methods, as such:
[ServiceContract]
public interface ICalculator
{
[OperationContract]
uint Divide(uint numerator, uint denominator);
}
static class Program
{
// Wrap those Begin/End methods into a Task-based API.
public static Task<uint> DivideAsyncTask(this CalculatorClient client,
uint numerator, uint denominator)
{
return Task<uint>.Factory.FromAsync(client.BeginDivide, client.EndDivide,
numerator, denominator, null);
}
static async Task CallCalculator()
{
var proxy = new CalculatorClient();
var task = proxy.DivideAsyncTask(10, 5);
var result = await task;
Console.WriteLine("Result: " + result);
}
static void Main(string[] args)
{
try
{
CallCalculator().Wait();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
}
Console.ReadKey();
}
}
You may be interested in one of my blog posts async WCF today and tomorrow - unfortunately, WP7 is still stuck in "today" mode. The Async CTP will probably be abandoned shortly now that VS2012 is out.