Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / WPF

Silverlight 3 & 4 Library Sharing with .NET 4.0 Library or WPF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Nov 2009CPOL4 min read 15K   3   2
Silverlight 3 & 4 library sharing with .NET 4.0 library or WPF

slwpfapplication

As if you need “another” reason to start running Visual Studio 2010 Beta 2, here is another for all the cross platform .NET Line of Business application developers.

Silverlight 3 & 4 Library assemblies can be referenced in .NET 4.0 applications and used.

The above WPF application has a file reference also known as a binary reference (as opposed to a project reference) to the BusinessEntities.Silverlight.dll and has instantiated the Customer class and used it for the DataContext for the above MainWindow.

How To Do It

The steps are simple, but you need to follow the workflow to avoid issues.

Now that Silverlight 4 has shipped and IDataErrorInfo has been added, this enables entity sharing between Silverlight and WPF much easier since you no longer have to implement this interface yourself just to get code compatibility with your business objects.

slwpfsolution

  1. Your Silverlight Library can't have any “Silverlight specific” code in it. I have been able to share my own framework code (Ocean) and business entities without any issues.
  2. Set up a file reference (as opposed to a project reference) between the .NET 4.0 project and your Silverlight 3 or 4 class library .dll. In the add reference dialog, use the Browse button to locate the target .dll.
  3. If you have the Silverlight 3 or 4 library in the same solution as the consuming .NET 4.0 project(s), it is important that you establish the correct project dependencies and build order. By default, Visual Studio will not alter your project build order when a file reference is made.

    In our case, we need the project build order to look like the below image:

    ProjectDependencies

    To establish the above build order, switch to the Dependencies tab, select the other solution projects that depend on the Silverlight 3 or 4 class library and check the Silverlight 3 or 4 library as pictured below:

    BuildOrder

A Few Thoughts

If you have never used file reference projects in your solution, a few things in the user experience are slightly different.

IntelliSense

If you edit the Silverlight 3 or 4 class library, don't expect those changes to be picked up by the .NET 4.0 project(s) until after you build. This means, if you added a property or method in the code editor, IntelliSense in the .NET 4.0 project won't “list it” until you build the solution.

Build Order

It's easy to overlook manually setting the project dependencies to ensure the correct build order since for all other projects you have ever created, Visual Studio did this for you.

Also, don't overlook this step after adding a new project to an existing solution that you've already set the build order on.

Clean Solution

If you have the Silverlight 3 or 4 class library in the solution and you clean the solution, the library assembly .dll will be removed from the output folder. This will cause Visual Studio to add some errors to your Error List since it can no long find the referenced assembly. After building the solution, these errors will be removed from the Error List.

So… if you clean your solution and send it to someone like on a blog post, developers that open your solution will see these errors. Again, build and all is well.

Service References

Now that you can put your business entities in a Silverlight 3 or 4 class library and your .NET 4.0 service, business and data layers can consume those entities, take full advantage of service reference dialog Advanced settings. Advanced button is located in the lower left corner of the Add Service Reference dialog.

I have had issues with the Add Service Reference feature when trying to get it to reuse my existing types. So I always elect to help the wizard and specifically select the assemblies that contain my business entities as I've done in the below image.

Note: The project download does not have a service reference, the below image is from another project.

ServiceReference

I love this feature because it allows me to use my business entities that have code in them in all the layers of my application without any duplication of code. This also easily enables entity sharing between Silverlight 3 OR 4 and WPF.

Download

The project source code can be downloaded from my SkyDrive.

Close

Hope you like the new developer productivity capabilities of Visual Studio 2010.

Have a great day,

Just a grain of sand on the world's beaches.

Posted in CodeProject, Silverlight, VB.NET, Visual Studio 2010, WPF General

License

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



Comments and Discussions

 
QuestionAlso working with .NET 3.5? Pin
Tobias Norlund9-Oct-10 11:04
Tobias Norlund9-Oct-10 11:04 
AnswerRe: Also working with .NET 3.5? Pin
User 27100911-Oct-10 3:09
User 27100911-Oct-10 3:09 

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.