Quantcast
Viewing all articles
Browse latest Browse all 20

Referencing class library projects with references from Silverlight application projects

One “feature” in VS2010 (and 2008 I assume) that bugs the hell out of me, is the way that referenced assemblies are packaged in Silverlight projects… And please let me know if you know of any fix for this!

Imagine that you have a solution with at least 2 projects. One Silverlight application project, and one class library project. The app project references the class library project as it contains functionality it needs. The class library project in turn references another assembly (or maybe more than one) that is needed for it to be able to run. This (or these) other assemblies are not default Silverlight assemblies, so they will not be on the target machine and needs to be included in the deployment… Unfortunately, this is where VS screws up…

As you build your project and run it, it explodes in your face. The reason? Well, the assemblies referenced by the class library project is not included in the Xap file. VS only looks one step away from the actual application project. It doesn’t walk the entire chain of references to make sure that all needed assemblies are actually included in the package.

So how do we fix it? Well, there are a couple of ways…or at least 2… One labour intensive and one plain ugly. I go for the plain ugly, as it is easier… but let’s first look at the heavy lifting way of doing it…

if you have all the time in the world, and do not care if you have to repeat a task every time you build your project, you can do it like this. Build the solution. Then open up the bin directory for the class library project in Windows Explorer and get a copy the required assemblies. Then open the bin directory for the application project and locate the application’s Xap file. Rename it to .zip and add the copied assemblies to it. And then finally, rename it back to .xap.

This can obviously be made a bit more efficient by hooking up the .xap extension to you favourite Zip library explorer, which removes a couple of steps. You could probably also automate the whole procedure using build events. But honestly…however you do it, it kind of sucks…

I assume that the other “solution” is the one that most people use. But that doesn’t make it right…it is still ugly…

To get VS to do the heavy lifting for you, just add a reference to the required external libraries to the application project. This ensures that VS includes the references when packaging the application.

And why do I find this to be an ugly solution? Well…it just feels dirty. Why does my application have to know about those extra assemblies? It’s like me having to know my grocer’s milk supplier to be able to purchase milk at my local store. I really don’t care who supplies the milk. I just want to know that my local grocer can get it for me. I don’t care where the milk comes from, just that it is at my grocery store when I need it. (Actually I sort of care. I prefer cow’s milk. I might not even notice a difference between cow’s milk and possum milk in a blind test, but I still prefer cow’s milk… I think…)

Ok…that was another rant from me. Probably less important topic than most…and a weirder analogy than usual (possibly even a little creepy), but I guess sitting at the office on the 27th of December makes me write about obvious things… But I still do want to highlight this somewhat annoying “feature” that has been added to VS “by design”.

And if anyone who matters happens for some reason to end up on my blog and read this. Can you please make sure that VS v.Next has a different “design” and uses a “feature” that walks the reference chain to make sure all required references are included in the Xap file. Please…

And if you happen to be a person that know of a better way to handle this problem, please let me know. Having my application ignorant about assemblies that are referenced by referenced assemblies (that is one awesome sentence), would make me sleep better at night. Even though it would mean that the application would not know for certain whether it was cow’s milk or possum milk that was being used to soak the cereal. But honestly…who cares…milk is milk… (I might take that back at some point…but we are all allowed to change our opinions about certain things)

That’s it for this time…sorry about the rant, but this “feature” has been bugging me and causing me problems on and off for quite some time now. I just had to get it off my chest…

Cheers!


Viewing all articles
Browse latest Browse all 20

Trending Articles