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