×
Namespaces

Variants
Actions

Portable Class Library with async / await support for Windows Phone

From Nokia Developer 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
Article
Created: arafattehsin (24 Mar 2014)
Last edited: influencer (25 Mar 2014)



Contents

Introduction

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.

Implementaton

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.

Dilemma

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", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="WCFServiceSample.IWCFSample")]
public interface IWCFSample {
 
[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IWCFSample/Login", ReplyAction="http://tempuri.org/IWCFSample/LoginResponse")]
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());
}
}

Note

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

Conclusion

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 04:22.
296 page views in the last 30 days.
×