7
votes

The project I'm working on is using a WCF Service on the server side (code is in the same project). I want to extend it by writing a PCL that consumes the service and would like to implement the calls with the Async/Await pattern.

As the PCL does not support Async/Await from the start I added the BCL.Async package via Nuget to the project. But whenever I try to generate the Proxy the Task based async client can not be selected i.e. generated. Adding the proxy manually via ChannelFactory also does not seem to be possible in the PCL.

My current approach is generating the proxy copying the ref class and then writing the async/await pattern on my own by using the Task wrapper provided by the TPL. But is there an easier way? How would I manually write the wrapper using the async/await pattern directly without generating multiple async patterns that have the same effect?

2
Do not use TPL/Task.Run to "wrap" your WCF calls. You aren't doing true threadless async, which assuming you are actually working on an embedded device, is going to make a huge difference. You can however use the Task.Factory.FromAsync(BeginXXX,EndXXX) method...that will work correctly. - Aron
@Aron, thanks for the input. The TPL link actually goes right to the method you mentioned so I guess that would be the right path, except I can somehow write/generate a task based wcf client.. - Mark
There are two ways I know. The first, is to use direct references (as opposed to WSDL) for your interfaces (which I recommend, when your client and server are developed from the same repo). The second, is to manually add the async methods using the partial keyword. - Aron
@Mark - did you get any proper answer ? can you share it with us? I'm also stuck with this scenario!... - Dennis
@Dennis I'm still not there.. But as soon as I have a working solution I'll post an update.. - Mark

2 Answers

3
votes

In the end I generated the proxy and manually wrapped the generated APM model with the TaskFactory to generate an Async/Await pattern in the client within the PCL. I wrote it down in this blog post.

Edit: Updated broken link.

0
votes

Have you tried using the /async parameter with the svcUtil.exe tool?

See more here ServiceModel Metadata Utility Tool (Svcutil.exe)