Async CTP is very nice to use.
I saw some examples for windows phone, all using:
(Assembly AsyncCtpLibrary_Phone.dll, v2.0.50727, AsyncCtpExtensions).
var client = new WebClient();
string data = await client.DownloadStringTaskAsync(new Uri(url));
or something like the above code.
How can I use methods with async / await for my services? (WCF)
Today all methods work this way:
service.MyRequestCompleted += service_myRequestCompleted;
service.MyRequestAsync();
I can not find a way to use extension methods for services.
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 theBegin*
/End*
methods, as such: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.
Unfortunetly, I haven't found a good way to generalize this for any event, but it's easy enough to adapt this for a particular event:
Then you can do:
I assume you see the pattern here, so you could adapt this so that it would work with some other event on some other type.
Oh, and if the event has arguments that are particularly important to you then you could make this return a
Task<T>
instead of aTask
and instead of usingtcs.SetResult(null)
you could use one of the parameters of the lambda to set the result.