Writing WPF, Silverlight, and WP7 applications it could be useful to share assemblies between these technologies. Since Silverlight 3 and .NET 4 it is indeed possible to not only share source code but share the same binary assemblies. While this wasn’t very intuitive to do with Visual Studio 2010 and Silverlight 3 and 4, now there are separate Portable Library Tools that make this process easier. This article describes how to share assemblies with these different technologies, and the limitations.
With Silverlight 3 and .NET 4 the format of assemblies is the same with these two technologies. However, of course there’s a difference between the Silverlight and the .NET assemblies. To limit the download sizes that are an important aspect of Silverlight, Silverlight assemblies are reduced in functionality compared to the .NET Framework. The .NET assemblies of Silverlight not only have fewer types included, but the types also have fewer members. For example, a simple core type such as the Enum class offers GetNames and GetValues methods to access all the names and values of an enumeration. These methods are not available with the Silverlight assemblies. A .NET programmer knowing the full-blown .NET environment switching to writing applications with Silverlight soon finds out some limitations. Writing assemblies that should be used with both Silverlight and WPF are restricted to the functionality available with both technologies.
To create shared assemblies between Silverlight and WPF it is possible to create a Silverlight class library.
Using this assembly from WPF is simply a matter of referencing the assembly. However, a project reference doesn’t work. A reference to the file is needed. Sharing entities or some algorithms can now be easily done with this library.
However, there’s an important limitation. Using assemblies within this shared assembly is limited to this list (for Silverlight 4 and .NET 4):
Information on this list is found here: https://blogs.msdn.com/b/clrteam/archive/2009/12/01/sharing-silverlight-assemblies-with-net-apps.aspx
By default a Silverlight class library has some more assemblies referenced. If, for example the type XmlWriter from the assembly System.Xml is used within the Silverlight class library, the code runs fine from a Silverlight application. With a .NET application compilation is successful as well; however running the application a FileNotFoundException is thrown as the System.Xml assembly cannot be found.
So using this feature to share assemblies between Silverlight and WPF with Visual Studio 2010 out of the box a lot of care needs to be taken for not using types or members that are not available across the different platforms.
Portable Library Tools
With the Portable Library Tools a solution to these issues is available. With these tools project settings are available to define what technologies the assemblies should be shared with, and warnings are given if types or members are used that are not available with all options.
The Portable Library Tools can be downloaded from the Visual Studio Gallery
and requires Visual Studio SP1 as prerequisite.
Creating a shared assembly, after the Portable Library Tools are installed, the project template to create a Portable Class Library is available.
With the project properties the library can be configured to define the target frameworks that should be used.
Depending on the selected target frameworks only a subset of assemblies is available that can be referenced.
The list of assemblies that is available with this tool is bigger than the list shown earlier.
However, the add reference doesn’t allow adding references to all these assemblies. This depends on the selection of the target frameworks. Changing the target frameworks to include just .NET Framework 4 and Silverlight 4, which can be done from the project properties,…
…the System.ComponentModel.Composition assembly is available as well for reference.
With the support of the Portable Library Tools sharing assemblies between different frameworks becomes easy. Just select the target frameworks, and the assemblies that can be used by the shared library is dealt with by the tool. This way it is easy to create entity assemblies and assemblies implementing algorithms that could be used across different frameworks.
More information in my Silverlight workshops