Click here to Skip to main content
Licence CPOL
First Posted 19 Nov 2009
Views 6,554
Downloads 0
Bookmarked 3 times

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

By Karl Shifflett | 19 Nov 2009 | Technical Blog
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.

1

2

3

4
1 vote, 100.0%
5
5.00/5 - 1 vote
μ 5.00, σa 5.00 [?]
A Technical Blog article. View original blog here.[^]

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.

  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. 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.

  3. 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.
  4. slwpfsolution

  5. 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)

About the Author

Karl Shifflett

Program Manager
Microsoft Corporation
United States United States

Member

Follow on Twitter Follow on Twitter
Karl loves .NET, WPF, WCF, ASP.NET, VB.NET and C#.
 
Awards:
 
  • December 2008 VB.NET Code Project Article Award
  • 2009 Code Project MVP
  • 2008 Code Project MVP
  • 2008 Microsoft MVP - Client App Dev
  • December 2007 VB.NET Code Project Article Award
  • Gold Medal Winner at IBM's 1998 PROIV Programming Contest in Las Vegas
Click here to check out my Blog
 
Click here to learn about Mole 2010 debugging tool for Visual Studio 2010
 
Click here to read about XAML Power Toys
 

Just a grain of sand on the worlds beaches.



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAlso working with .NET 3.5? PinmemberTobias Norlund12:04 9 Oct '10  
AnswerRe: Also working with .NET 3.5? PinmemberKarl Shifflett4:09 11 Oct '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 19 Nov 2009
Article Copyright 2009 by Karl Shifflett
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid