Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Portable Class Library with async / await support for Windows Phone

From Wiki
Jump to: navigation, search

This article explains how can we use task based asynchronous pattern in our Windows Phone apps.

Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820
Created: arafattehsin (24 Mar 2014)
Last edited: influencer (25 Mar 2014)



Portable Class Library (PCL) is an ease from all the pain of writing code again; it bridges the difference of API sets, enables cross platform development by supporting a subset of assemblies from .NET Framework, Silverlight, .NET for Windows Store apps, Windows Phone and Xbox 360.

You can write a surprising amount of portable code with the subset of supported APIs. Everything from business logic to web service client proxies (will discuss later) can be portable. This enables you to implement most, if not all, application logic in portable assemblies and then they can be used in apps that have form-factor specific UIs for the chosen platforms.

Portable Class Library.


The solution of below problem isn't very difficult but it surely has got a little limitation as of now. Let's start digging into it.


When a WCF (Windows Communication Foundation) service is added as a reference for Windows 8 / 8.1 apps, the generated proxy class is based upon Task Based Asynchronous Pattern (TAP - which is an asynchronous design pattern for new development) whereas in Windows Phone 8, proxy class is generated with Asynchronous Programming Model (APM). So, when you create PCL that supports both Windows 8 and Windows Phone then you would always get a client proxy based on the APM.

What! Why?

The reason why proxy was generated using APM pattern because the code base for Windows Phone 8 is still on Silverlight which does not have a support for TAP. Therefore, if you try to mock up the interface with TAP you will always encounter generosity, an exception.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "")]
public interface IWCFSample {
[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="", ReplyAction="")]
System.IAsyncResult BeginLogin(string username, string password, System.AsyncCallback callback, object asyncState);
bool EndLogin(System.IAsyncResult result);

This isn't an end

Despite the limitation by PCL, you should not lose hope and stick to your goal. There’s still a hope and that hope is called Task Parallel Library (TPL) which allows a conversion from APM operations to TAP. You can convert the methods of APM to task based operation using TaskFactory’s FromAsync method.

public interface IAsyncPCL
Task<bool> LoginAsync(string username, string password);
public class AsyncPCL : IAsyncPCL
public Task<bool> LoginAsync(string username, string password)
WCFServiceSample.IWCFSample service = new WCFServiceSample.WCFSampleClient();
return new TaskFactory().FromAsync<string, string, bool>(service.BeginLogin, service.EndLogin, username, password, new object());


There are still couple of things you need to consider;

  1. Indicate the parameters type along with the return type in generics definition of FromAsync method. Return type should always be the last one
  2. There’s a restriction of three parameters at most by the definition of FromAsync. However, you can always pass a custom data object to play more

Complete Code


Portable Class Library is miraculously useful but on the other hand, it has got significant limitations. I would like to thank one of my friends Mark Allibone for highlighting the way of asynchronous calls from PCL.

In spite of limitations with PCL, it is still recommended for a lot of code re-usability with not only Windows Phone, Windows 8 but you can also develop Nokia X apps by using that. Stay in touch for the upcoming articles and hopefully I will take you there.

This page was last modified on 25 March 2014, at 01:22.
157 page views in the last 30 days.