Adil's Tech Weblog

Demystifying software engineering and everything in between

By

Portable Class Library in .NET

At times you must have came across a situation when you want to share class library (DLLs) across different target platform.  For instance , I faced a situation where I wanted to create a class library and share business logic with WCF service as well as Silverlight application. This means that we want our class library to target different version of framework such as Silverlight. Same is the case with Windows Phone.

Previously, if you have made any class library project containing something that you would like to share across your Silverlight app or WP app or Xbox or web app, Visual Studio would not allow you to add reference of class library project (DLL) in another project targeting other version of .NET framework. The workaround involve rework/duplication resulting in creating multiple projects.

Visual Studio 11 (Beta) has introduced a new project template “Portable Class library” which allows you to create a portable DLL that can be ported with different version of framework ;)

Portable Class Library Template

If you look at the project properties (Alt+Enter), you have option to “Change” or “Select” target frameworks.

Change Target Frameworks

The concept behind the scene is very simple for the reason why it was restricted previously and why it is allowed via this template. Previously, a class library targeting .NET Framework (server) does include references such as System.Data, System.ServiceModel which obviously are not supported in Silverlight runtime or on Windows Phone etc. And as you can anticipate what this new template does is that it restricts you to common references (such as System.dll, System.Core.dll, System.ServiceModel.dll), which can be referred to all versions of .NET framework.

You can also get this template in VS 2010 SP1 and its available via Visual Studio extensions.

Portable Library in VS 2010

In short, Visual Studio 11 has new project template that allows you to create a portable library that can be referred across different target platforms of .NET framework.

There is a recommended article “Create a Continuous Client Using Portable Class Libraries” in MSDN magazine explaining the use of Portable library.

Feel free to share your similar experience and usage of Portable class library in comments form below.

2 Responses to Portable Class Library in .NET

  1. Anonymous says:

    It would be nice if you could include System.Data classes for when it’s used with the full framework, but when it’s referenced by say, Windows Phone the IDE and/or the framework just makes those classes unavailable for use.

  2. Pingback: Sharing Code between Windows Store/Phone Apps using MVVM and PCL

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.