Click here to Skip to main content
15,112,252 members
Articles / Programming Languages / XML
Posted 18 Jan 2009


70 bookmarked

Converting .NET Assemblies to Silverlight Assemblies

Rate me:
Please Sign up or sign in to vote.
4.92/5 (15 votes)
3 Mar 2009CPOL5 min read
Share your .NET assemblies with Silverlight.



This article comes to solve the issue presented by David Betz in his article Reusing .NET Assemblies in Silverlight by creating a simple Console application that can be run in the Post-Build commands.


Now that Silverlight has entered its second version and is starting to be implemented by a larger number of developers, more advanced applications are being developed on the framework.

One of the powerful features of Silverlight is its connectivity with WCF Services. As most architects choose to implement the contract classes in a public assembly that is then referenced by both the Service Implementor and the Client, an issue occurs when attempting to reference this public assembly through a Silverlight application.

I won't go into too much detail as to why Visual Studio doesn't allow you to add non Silverlight assemblies to a Silverlight project since this is presented in great depth in the article posted by David Betz on his NeFX harmonics site. But, in a nutshell, the assemblies that Silverlight uses (System.dll, mscorlib.dll etc.) are not the same as the basic .NET assemblies although many of the functions exist in both. You could say that the Silverlight assemblies are a slim-down version of the full .NET assemblies, but are almost identical in the functions both expose.

So, basically, if we could modify our public assembly to reference the Silverlight version of these assemblies without modifying the code, we could re-use our public assembly in our Silverlight app.

MSIL Assemblies

Basically, when you write you code in any .NET language (C#, VB.NET), the compiler compiles the code to what is known as MSIL or Microsoft Intermediate Language. This code is then assembled into a .NET Assembly that gets loaded and JIT'ed at runtime. Since assembly references are only validated at runtime, we can safely modify the reference of the problematic assemblies to the correct Silverlight assemblies in the IL code, and when the Assembly is loaded through Silverlight, the correct assemblies will already be in memory and the JIT compiler will compile our code against it.

Our Target

So, to keep things simple, what we want to do basically is once the assembly is built, disassemble it back into MSIL, make a few changes in the references (maybe remove some attributes that are not supported by the Silverlight version of the references), and re-assemble the code back into a new assembly that Silverlight can use.

Note: the new assembly we will create can only be used from a Silverlight project, but all the Type signatures will be identical to our original assembly, and so we will be able to share the WCF contracts between the two.

Setting up the Application

The code included is a simple Console application written in C#. It is accompanied by an XML file containing all the settings it needs to run.

Basically, the application needs to know the following information:

  1. The location of the MSIL Assembler and Disassembler (ilasm.exe and ildasm.exe).
  2. A list of assemblies that alter between their Silverlight implementation and their .NET counterparts.
  3. A list of any non-supported attributes located in the original .NET assemblies and that do not exist in the Silverlight ones that must be removed from the IL code.

The XML file looks something like this:

<?xml version="1.0" encoding="utf-8" ?>
<SLAsm.Settings xmlns="urn:Silverlight-Assmblies/SLAsmSettings.xsd"
    ILDisassembler="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe">
     <ExternAssembly Name="System" Version="" 
         PublicKeyToken="7C EC 85 D7 BE A7 79 8E" />
     <ExternAssembly Name="mscorlib" Version="" 
         PublicKeyToken="7C EC 85 D7 BE A7 79 8E" />
     <ExternAssembly Name="System.Core" Version="" 
         PublicKeyToken="7C EC 85 D7 BE A7 79 8E" />
     <ExternAssembly Name="System.Net" Version="" 
         PublicKeyToken="7C EC 85 D7 BE A7 79 8E" />
     <ExternAssembly Name="System.Runtime.Serialization" 
         Version="" PublicKeyToken="7C EC 85 D7 BE A7 79 8E" />
     <ExternAssembly Name="System.Windows" 
         Version="" PublicKeyToken="7C EC 85 D7 BE A7 79 8E" />
     <ExternAssembly Name="System.Json" 
         Version="" PublicKeyToken="31 BF 38 56 AD 36 4E 35" />
     <ExternAssembly Name="System.ServiceModel" 
         Version="" PublicKeyToken="31 BF 38 56 AD 36 4E 35" >

Remember to set the correct paths to the ILAsm.exe and ILdasm.exe files

I have pre-populated the file with all the Silverlight assembly references and added a list of unsupported attributes to the System.ServiceModel assembly.

Note: this is not a complete list of unsuported attributes, rather the ones I have run into myself. Feel free to add any attributes you find that are not working with Silverlight. (You know when your Silverlight app throws a TypeLoadException when it attempts to load your assembly.)

Using the Code

Now that you have set up the XML file, you will probably want to use the code. The application is a simple console app that takes the following arguments:

<AssemblyPath.dll> [/Key=<StrongNameKeyPath.snk>] [/Output=<TargetPath.dll>]

where AssemblyPath.dll is the path to your public .NET assembly. The optional /Output switch specifies where you want to store your new Silverlight assembly. (If you leave this blank, the output file will be AssemblyPath.SL.dll.)

And finally, since we messed all the original IL code, if the original assembly was signed with a strong name key, the newly created assembly will not be properly signed, so you can include the path to the original .snk file you used to sign your assembly, and the app will put it all back together

Setting up as a Post-Build Event

The best way to make sure your public assembly gets converted into a Silverlight compatible one and stays updated whenever you modify it is to add a Post-Build command to your project. To do this, right click on your project and click on the 'Project Properties' menu item.

Navigate to the 'Build Events' tab:


Now, click on the 'Edit Post-build' command, and a dialog opens:


Enter the path to our converter application followed by the "$(TargetPath)" macro to specify the built assembly as the file that will be converted. Add any extra arguments (like the /Output= and /Key=) you need and click on 'OK'.

In the 'Build Events' tab, make sure the option for 'Run the post-build event:' is set to 'On successful build'. Save the project. From here on, whenever your project builds successfully, Visual-Studio will run our app and create a Silverlight compatible assembly of the project.

Note: if the creation of the compatible assembly fails, the entire project will be marked as 'Build Failed', and you will see the error message returned by ilasm.exe in your output window.


I hope this is useful to anyone out there that may have hit the same issue, and if it did help, be sure to drop me a line...


  • 2009 01 22 - Updated source to load ilasm.exe from the .NET installation folder, also included ildasm.exe in the solution for those of you who don't have the .NET SDK installed.
  • 2009 03 03 - Updated the Remove unsupported attributes feature to support methods with more than one attribute (as pointed out by Boris Modylevsky)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Suriel Bendahan
Software Developer (Senior) VCM Software
Israel Israel
No Biography provided

Comments and Discussions

QuestionThank you Pin
Deva0212-Dec-14 2:28
MemberDeva0212-Dec-14 2:28 
GeneralThank you Pin
tomi_slav12-Feb-14 6:29
Membertomi_slav12-Feb-14 6:29 
BugGot an error after conversion, Pin
senthil_sivanth25-May-13 6:27
Membersenthil_sivanth25-May-13 6:27 
GeneralHelped Pin
Santopk28-Dec-11 2:21
MemberSantopk28-Dec-11 2:21 
GeneralHelped with XNA4 development...thanks! Pin
yyr5769525-May-11 10:03
Memberyyr5769525-May-11 10:03 
GeneralAdd Third Party Reference in Silver Light Pin
Anandan220116-Jul-10 6:14
MemberAnandan220116-Jul-10 6:14 
GeneralRe: Add Third Party Reference in Silver Light Pin
reejesh.ravindran3-Sep-10 3:35
Memberreejesh.ravindran3-Sep-10 3:35 
Jeremy Schaab6-Apr-10 5:56
MemberJeremy Schaab6-Apr-10 5:56 
Generalerror -- Failed to open key file '/key=My.snk': 0x0000007B Pin
MarinaGre16-Feb-10 21:36
MemberMarinaGre16-Feb-10 21:36 
Question.NET 4 / Silverlight 3? Pin
Paul Swennenhuis13-Jan-10 5:01
MemberPaul Swennenhuis13-Jan-10 5:01 
GeneralHelp!! - syntax error at token 'System.Core' Pin
justintoth12-Jun-09 12:54
Memberjustintoth12-Jun-09 12:54 
GeneralRe: Help!! - syntax error at token 'System.Core' Pin
Suriel Bendahan19-Jun-09 4:19
MemberSuriel Bendahan19-Jun-09 4:19 
GeneralRe: Help!! - syntax error at token 'System.Core' Pin
justintoth19-Jun-09 5:59
Memberjustintoth19-Jun-09 5:59 
QuestionAny change to use this using Silverlight 3.0? Pin
Pinho21-May-09 5:31
MemberPinho21-May-09 5:31 
Generalgetting error in while re-assembling... Pin
Supriya Tonape9-Apr-09 4:47
MemberSupriya Tonape9-Apr-09 4:47 
GeneralRe: getting error in while re-assembling... Pin
dave pollot30-Jun-09 8:34
Memberdave pollot30-Jun-09 8:34 
GeneralMFC Dll Pin
VCsamir30-Mar-09 2:20
MemberVCsamir30-Mar-09 2:20 
QuestionHow to convert enterprise library dll's to Silverlight assembly Pin
Rupanshut24-Mar-09 2:45
MemberRupanshut24-Mar-09 2:45 
QuestionAlternative approach? Pin
Ravi Bhavnani3-Mar-09 12:01
professionalRavi Bhavnani3-Mar-09 12:01 
AnswerRe: Alternative approach? Pin
Suriel Bendahan3-Mar-09 12:19
MemberSuriel Bendahan3-Mar-09 12:19 
GeneralWonderful post and excellent tool Pin
Boris Modylevsky1-Mar-09 6:34
MemberBoris Modylevsky1-Mar-09 6:34 
GeneralRe: Wonderful post and excellent tool Pin
Suriel Bendahan1-Mar-09 10:21
MemberSuriel Bendahan1-Mar-09 10:21 
GeneralRe: Wonderful post and excellent tool Pin
Boris Modylevsky4-Mar-09 3:11
MemberBoris Modylevsky4-Mar-09 3:11 
GeneralRe: Wonderful post and excellent tool Pin
Boris Modylevsky4-Mar-09 3:54
MemberBoris Modylevsky4-Mar-09 3:54 
GeneralRe: Wonderful post and excellent tool Pin
Suriel Bendahan4-Mar-09 6:53
MemberSuriel Bendahan4-Mar-09 6:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.