Silverlight 2.0: You can only add project references to other Silverlight projects in the solution.
When creating Silverlight 2.0 Beta applications, you will receive the following message if you try to reference non-Silverlight projects in the same solution:
You can only add project references to other Silverlight projects in the solution.
If you attempt to use "Add Reference->Browse…", and select the DLL manually, you will receive:
You can’t add a reference to <DLL> as it was not built against the Silverlight runtime. Silverlight projects will only work with Silverlight assemblies.
It works the other way too. A WPF application, for example, can’t reference a Silverlight class library:
Silverlight projects can only be referenced by other Silverlight projects.
It seems a bit painful. One solution would be to reference your C#/VB.NET files from two separate projects, but that’s not so much fun. Here is my workaround:
- Do a rebuild on your DLL project. In the Output window, locate the line that calls CSC.exe/VBC.exe.
- Copy and paste that whole command into Notepad.
- Insert "/nostdlib+" into the arguments.
- Look for /reference elements that refer to .NET Framework DLL’s. Replace the folder of the DLL reference so that it becomes:
/reference:"C:\Program Files\Microsoft Silverlight\2.0.30226.2\<DLL NAME>.dll" - Change the /out setting to point to a different folder. I used:
/out:bin/Silverlight/$(TargetFileName) - In the /define setting, add SILVERLIGHT, i.e.,:
/define:TRACE;DEBUG;SILVERLIGHT - The command you got from the Visual Studio Output window will likely include the file names of all of the C# files in your project. This will mean that you need to update your command each time you add new files. For a better experience, replace all of the file names with the following:
/recurse:*.cs
Now take what you’ve edited in Notepad, and insert the following at the top:
cd $(ProjectDir)
Then copy and paste the whole thing into the Post-build event command line in the project settings for the property:
Now build your solution. Any errors, such as classes or methods not supported by the Silverlight libraries, will appear in the Errors window just like the regular compiler:
You should now be able to reference the Silverlight version of your DLL from your Silverlight application. You will need to use the Add Reference…->Browse… dialog, and find the folder that you told the compiler to send the DLL’s.
Like me, you’re probably thinking this is a bit clunky. In the future I would love to see regular .NET projects have the ability to "target" Silverlight. If I can target x86 or x64, I should be able to setup a target for Silverlight too
Filed under: Silverlight

[…] Silverlight 2: You Can Only Add Project References to Other Silverlight Projects in the Solution (Paul Stovell) […]
Paul,
Awesome post! This was a huge help to me, and something I couldn’t have figured out without the assistance. For other readers here are some of the problems I came across and my solutions to them:
1. I had to remove references I didn’t need that don’t exist in Silverlight like “System.Data.DataSetExtensions.dll”
2. Had to add quotes around some references like system.dll
3. Got: “Predefined type ‘System.Object’ is not defined or imported” so added reference to mscorlib.dll.
4. Got: “Unexpected error creating debug information file … The system cannot find the path specified”, so I removed the /debug+ and /debug:full arguments and the DEBUG; out of the /define argument. I don’t really need debugging, so I didn’t put any time into really fixing this — removing debugging was fine.
5. Got: “Cannot create temporary file ‘…\bin\silverlight’ — The directory name is invalid.” I had to create the Silverlight directory under bin. I guess VS won’t do it for you.
I tried this, but it didn’t completey work. Is it possible that this ‘hack’ does not work with System.XML for serialization and such? I don’t seem to be able to get those right.
Thanks.
Hello,
We also have the same problem but we are not able to fix that issue..
It will be usefull if u give us a sample code..
Hey Paul,
Great post!
I changed a couple of things with Beta 2 out now.
First, the directory I used was “C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies” and not the “C:\Program Files\Microsoft Silverlight\2.0.30523.6″ one. I looked at one of my SilverLight projects and that’s the path they use to reference assemblies. Does that seem like a good idea to you?
Also, make sure that the reference file path is in quotes if there’s a space in it such as “Program Files”. The compiler chokes if its not included.
Finally, I had to add a brand new reference to even get going and that was “c:\Program Files\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies\mscorlib.dll”. It contains the base CLR stuff such as System.Object.
Great post again!!
R/Joel
Interesting approach.
I usually share the source files and create two projects in VS. This allows some extra flexibility since sometimes I want some files for the SL version and not the Desktop runtime or viceversa, but it means know I have to projects in the solution and have to remember to add the files add the link from one file to the other.
Hi Miguel,
I’m using #ifdef SILVERLIGHT for that
but in my case I’m trying to port the whole library not just a subset.