Integrating Twitter Feeds in Web Applications
Real World .NET 4 and C#

Sharing Assemblies between Silverlight and .NET

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.

Sharing Assemblies

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.

image

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):

  • Mscorlib
  • System
  • System.Core
  • System.ComponentModel.Composition
  • Microsoft.VisualBasic

Information on this list is found here: http://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.

image

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

http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981/

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.

image

With the project properties the library can be configured to define the target frameworks that should be used.

image

Depending on the selected target frameworks only a subset of assemblies is available that can be referenced.

image

The list of assemblies that is available with this tool is bigger than the list shown earlier.

  • Mscorlib
  • System
  • System.Core
  • System.Xml
  • System.ComponentModel.Composition
  • System.Net
  • System.Runtime.Serialization
  • System.ServiceModel
  • System.Xml.Serialization
  • System.Windows

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,…

image

…the System.ComponentModel.Composition assembly is available as well for reference.

image

 

Summary

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.

Christian

CN innovation

More information in my Silverlight workshops

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

AddisonMason

Individual Convenient Collection Resources that make this process simpler and here explains how to discuss devices with these different technology, and the restrictions.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)