Silverlight 2 dynamic assembly loading - Part 1

30 01 2009

In this article:
- Rebuilding the AppManifest.xaml
- (Re)compressing .xap files with 7zip
Coming in part 2 (Click to open):
- Loading the assemblies.

Silverlight 2 by default packages all the referenced assemblies into one big xap file. This is a nice for deployment scenarios, but when you have a lot of Silverlight controls, using the same assemblies over and over again, you waste a lot of bandwidth, and user experience drops because of the extra time it takes to load a page.

imageDo not copy local

Some of it can be achieved by setting the “copy local” property of the referenced assemblies to false. This tells the packager not to add them to the .xap file automatically.

Now when the Silverlight application starts in a browser we will have to resolve the “missing” referenced assemblies. I had hoped to use the AppDomain.AssemblyResolve event, but as it seems, this has been marked with the [SECURITY CRITICAL] attribute, which means it can only be used in the .NET framework itself.

Luckily the assemblies are accessed only when needed, so the assembly is only needed when you first access something from the assembly, like a class or method. This means that if we do all the loading in the App.xaml.cs we should be fine. But for this example I will not use this option, so you can put them back to True.

Because I’m Lazy

I am a lazy programmer ;) And I do not want to manually load every assembly for every Silverlight project. I’d rather do some more work now, and save myself (and hopefully you as well) lots of time in the future. And as an added bonus we get some extra compression (+- 20%) in our .xap files, because the normal .xap packager does almost no compression at all. And if you would use the Copy Local option above you would get no compression at all for your referenced assemblies.

To get an idea of what I’m talking about, take a look at the following diagram:

image

What you are seeing above, is the default packaging on the left, and the ReXapped packaging on the right. I also added the size in Kb. To make the difference a bit more drastic, I put 2 images in MyLibrary, which is packaged twice on the left and only once on the right. This also goes for the System.Xml.Linq.dll assembly. The rest of the reduction in size is due to the fact that 7Zip just does better compression. And if you have more then 2 Applications, and you have lots of shared code and images , which in my experience is usually the case, the reduction will be even greater.

ReXapper

Let met start out by saying that the ReXapper name is one I did not think up myself, I have seen it many times already around the web, but why change a good name, right?

The ReXapper is a Console application, which is run After Build on both MySilverlightApplications. It receives the parameters from the build events, and starts ReXapping the applications.

It should be run with the following parameters:
ReXapper.exe [7ZipLocation] [projectName] [projectTargetDir] [silverlightTargetDir]

When used in the After Build event it would be:
$(SolutionDir)ReXapper\bin\debug\ReXapper.exe
$(SolutionDir)7za.exe
$(ProjectName)
$(TargetDir)
$(SolutionDir)DynamicAssemblyLoading.Web\ClientBin

The code for the Rexapper is a bit too big to completely go over right now, but here are a few higlights. You can download the full source code below.

Saving AppManifest.xaml from an XDocument

The AppManifest does not have a xml starting tag. If added, the Silverlight Plugin will fail to load, and of course <?xml is added by default when saving a XDocument :)

The solution is using the XMLWriter with XmlWritersettings having OmitDeclaration = true. Check the SaveXDocumentWithoutXmlDeclaration method in the code.

Changing and not creating the AppManifest

Because I ran into a few problems, creating a new AppManifest from scratch I decided to reuse the ogirinal one and change it to meet the needs.

Lots of Linq and Lambdas

I have become such a fan of Linq and Lambdas that you will see me using it wherever I can, probably even in places where it’s not necessary, I just love to use them :)

Next

Now we have the new xap files, we can run the application and see that they work. Of course you cannot yet use the Referenced assemblies like the System.Linq.Xml and MyLibrary, but because they are not yet used in the code, Silverlight will not trow any errors.

In Part 2, ready hopefully somewhere next week, we will read our custom created ReferencedAssemblies.xaml from our xap, load all the referenced assemblies from their respective .xap files, and use resources from MyLibrary in our Applications.

Click here to download the source code in it’s current state.

Stay in the light!

Robertjan Tuit

Technorati Tags:

*UPDATE: Click here to view part 2



Software Development Conference 2008

20 09 2008

The 6th and 7th of October, we are organizing our 17th! SDN Conference. 2 days of international and national speakers. Be sure to be there for I believe that this year is going to be one of the best ones we ever did!

So if you haven’t gotten a ticket yet get it here

See you there!

.



MSN Video ListBox Sources

27 06 2008

Just a few hours ago, the expression newsletter for June was released with an article I wrote. But the source code downloads do not seem to work, I hope that it will be fixed as soon as possible, but for nor here are the source code downloads:

Stay in the light!

Robertjan Tuit



Silverlight 2 Beta2

7 06 2008

The Beta 2 is a big improvement from the beta 1. For the whole list of improvements click here. But to take a few that I found interesting:

Especially looking forward to the WCF push mechanism. For chat, games or even enterprise application notification. And the Visual State Manager is going to make my work a whole lot easier.

Will try to go into more details the following weeks.

I’m off to see how much of my SL2 applications don’t work anymore :)

SL2 B2 is Compatible with 1.0 but not with the B1. So when a user visits a website with a B2 application, he will be prompted to install B2. All B1 sites from that moment on will not work anymore. So you need to update your B1 applications soon. Read the following posts from Shawn Wildermuth for more information on updating.

Stay in the light!

Robertjan Tuit



Silverlight Mopup - Part 3

8 05 2008

Robertjan Tuit

Technorati tags: ,


Zune Game Development with XNA

8 05 2008

When I first saw the title, I thought they were making a joke. But it is true. With the next version of XNA Game Studio you can develop games for your zune!.

XNA 3.0 CTP works with all C# versions of Visual Studio XNA2008, it works only for Zune and Windows development (Not xbox develkopment yet) and it has no 64bit support.

Let me know if you have created your first cool game, I’ll be the first to add it to my Zune :).



InTrack Usability Slides and Demos

11 04 2008

Here are the slides and demos for the InTrack Session. I did remove the deepzoom demo, because it was too large.

The reason the files are still so big, is because I did leave the video files in. But with a bit of a connection this shouldn’t be a problem.

For a great deepzoom demo visit:

Robertjan Tuit



I ? Unicode

29 03 2008

I’m a regular reader of www.CodingHoror.com/blog. I could go as far to say that I’m kind of a fan. One of his lasts posts made me laugh so much that I had to share it with you. The images below are available as bumper stickers and t-shirts:

I {entity} Unicode (Mac)

I {entity} Unicode PC

As a developer you should be laughing right now, because if you are not: find another job :)

If your not a developer: ask one, and see him struggling to explain :)

Robertjan Tuit



Stupid Me

26 03 2008

Yes, you read it correctly, I am a very very stupid man. Let me explain.

About 7 months ago I purchased one of those on-line backup accounts. My intention was to put all my important data there, all my documents, photos etc. I had seen some hard disk crashes and stolen laptops and had decided that that was not the situation I wanted to get into.

And I really meant it at the time, but I was too busy, not making time for it and kept putting it off.

And guess what? my hard drive crashed! And it crashed big time, windows sees it sometimes, sometimes not, sometimes I see something that resembles a directory structure and again sometimes it shows nothing. But the data is inaccessible.

I have tried a wide variety of tools and boot cd’s including PCInspector, Ultimate BootCD and Rescue Disk. And have tried to access it internally and trough a USB casing and even in Linux and Dos. I’m kind of out of options. It doesn’t sound very broken but the computer doesn’t really now what to do with it.

Now I have moved to the Data Recovery companies, yes it is expensive, but that is what the data is worth to me. There are a couple, but I wanted a Dutch speaking one, I have singled out an international (Spanish) company that has a Dutch office: Aigon.

I have spoken with them and they seem OK, but I was kind of wondering if anyone had any experience with them or with other companies doing this work. Like to hear from you and your experiences.

This is not something you can try a few times :).

So if you have any ideas, all the input is very much welcome.

Robertjan "Very Stupid" Tuit



Silverlight Mopup part 2 of many

23 03 2008

I promised myself to not do this mopup too frequently, but is has been long enough I guess. There are lot’s of cool new Silverlight 2 samples and projects out there, a small selection:

  • Deep Zoom Collection
    We have seen many deep zoom samples of one large image, but what if you wantDeepZoomCollection control over the placement of the images after the deep zoom composer has exported it’s data. A great sample from the Expression Team.

  • DataGrid Sample from Mix08
    One of the most interesting sessions at mix, was the sessions done by Joe Stegman and Mike Harsh. They published al their material almost directly, but left out the DataGrid sample. They got lot’s of requests for it and have put it up on for download.

  • More on Routing and Bubbling
    Because the routing and bubbling is different from WPF, it left a lot of people confused. Jessie tries to clarify it al, and even tries to explain some of the decisions made.

More to come, stay in the light!

Robertjan Tuit