Pre-Loading Silverlight Prism Modules & Assemblies

I was developing a Unity/Prism Silverlight enterprise application which used many shared assemblies and several modules. I wanted to show a ‘startup progress’ for the core assemblies so the application would have a better user experience, especially for the users’ first few moments of the application.


The Silverlight plugin downloader only reported the progress for the shell. User experience was very poor due to the lack of a real gauge of the total progress. Even though the entire application was about 7 MB, the shell was only 500 KB. This resulted in the built-in progress bar reaching 100% very quickly, way before the actual download had reached 10%. This was not really a problem on faster connections, but on slower connections, there was a long delay before the application really started. This delay exists because the shell is finished downloading, but Silverlight is still downloading all the other assemblies and modules. These other assemblies make up the main part of the application, and thus is more important compared to the shell.

So what I wanted to do was to find some way to hook into the actual Silverlight download requests of all the files and then use this instead of the built-in progress reporting. However there doesn’t seem to be any way to do this from JavaScript.


I know that almost all browsers support caching and subsequent loads from the cache are almost instantaneous. I can use this to my advantage. So what I did was to pre-download the files that would have been downloaded if I was using the built-in downloader. Then, when the Silverlight control started its own download, all the required files would be in the browser cache. This allows the application to no longer need to download the files, and the application startup would be very quick, thus not needing a Silverlight progress section here.


This solution calls for several things:

  • I don’t want any hardcoded values, such as sizes, filenames or urls.
  • The splash screen should be seamless with the actual Silverlight application
  • JavaScript downloader should start and then switch to the Silverlight control once it is finished.

The first thing that I needed to do on the server was to create a way for my JavaScript code to get a list of all the files and their sizes that it needed to download. Just to keep things simple, I created a simple ASP.NET Generic Handler that returned a JSON string of all the files. Because Silverlight downloads the files in our “ClientBin” folder, my handler simply enumerates the files in this directory and returns this. I also didn’t want to build up this resulting string manually, so I used a DataContractJsonSerializer that serializes the array of files – I created the returning types as internal classes of the handler.

On the client, I can request the list then calculate the total size for the progress and start downloading. As each file finishes downloading, I can update the progress message and it will be more accurate to what is really happening.

The splash screen is a very simple HTML div that is displayed over the actual Silverlight object. The Silverlight object is actually hidden by default using CSS styles, to prevent it from automatically starting up. When my JavaScript reports that all the files are downloaded, I hide the splash screen div and show the Silverlight control. This provides a very accurate, user-friendly way of reporting progress. And when the download reaches 100%, all the required files are all finished and on the client.

Notes / Observations

I did have a problem when I pre-downloaded the shell xap file: Silverlight did not start the actual application. So to work around this I decided to downloadd all the files, except the shell and let Silverlight do this one. Now this caused slight problems in my download mechanism: First the progress would be off slightly and I would have no way of reporting this shell progress. Silverlight has JavaScript callbacks for the shell downloads: the onSourceDownloadProgressChanged function.

Because I know the size of the shell – from my handler, and I would have notifications from Silverlight about this file’s progress, I could then combine this with my progress message to get a nice value. So effectively, my download stops at 90% and then the Silverlight control will take over and notify me of the shell’s progress which I work out how far it should really be.

Currently I am using both the HTML element and the Silverlight mechanism for splash screens. For the first bit, I use the HTML element and when the Silverlight takes over, I hide it and the Silverlight (xaml) version shows. Both are exactly the same so the user does not notice the switch at all.


This is the server-side handler. The Browser Cache mechanism for the urls are case sensitive, so remember to use the case that Silverlight is going to use for it’s requests.

public class BrandDirectorFilesHandler : IHttpHandler
    public void ProcessRequest(HttpContext context)
        context.Response.ContentType = "text/plain";

        var serializer = new DataContractJsonSerializer(

    private static DownloadableFileResponse GetFilesRoot()
        // logic to read the file list from the file system

Here is the resulting objects that I serialize for the client.

internal class DownloadableFileResponse
    internal IList<DownloadableFile> Files { get; set; }

    internal long TotalSize { get; set; }

internal class DownloadableFile
    internal string Url { get; set; }

    internal long Size { get; set; }

This is the part of the whole system that controls the downloads of the files from the client side.

var totalSize = 0;
var progress = 0;
var xapFile = null;

// Silverlight default functions
function onSourceDownloadProgressChanged(sender, eventArgs) {
    if (xapFile === null) {
        // there may have been a problem obtaing the file list
        // so create a dummy state for the silverlight to take 
        // over from - these values are just the last known 
        // values from a previous run that I did
        totalSize = 7000000;
        progress = totalSize * 0.88;
        xapFile = {
            Size: 700000

    var shellProgress = progress + (eventArgs.progress * xapFile.Size);
    var shellPercent = shellProgress / totalSize;
    var text = Math.round(shellPercent * 100).toString();

    // this bit is for the actual Silverlight splash.xaml
    sender.findName("ProgressText").Text = text;
    // this is the html element
    // I do both just to be sure

// the javascript for pre-caching the assemblies
$(function () {
    // get this apps root
    var urlParts = location.href.split('?');
    var mainUrl = urlParts[0];
    mainUrl = mainUrl.substring(0, mainUrl.lastIndexOf('/'));

    // download the list of files from the handler
        url: mainUrl + '/BrandDirectorFilesHandler.ashx',
        success: function (data) {
            var response = $.parseJSON(data);

            // set the total size for the silverlight bit
            totalSize = response.TotalSize;

            // don't download the shell xap file
            // get the xap filename from the Silverlight DOM element
            var shellXapPath =  
                .filter(function (index, child) {
                    return === "source"; 
            // find the shel in the list from our server
            xapFile =
                .filter(function (item) {
                    return shellXapPath.indexOf(item.Url) !== -1;

            // start downloads
        error: function () {
            // default to silverlight on any errors getting the list

// all downloads are complete, show the silverlight control
function onAllDownloadsComplete() {

// initiate the download for each file
function downloadFile(index, file) {
    if (file !== xapFile) {
            url: new String(file.Url).toString(),
            complete: function () {
                // download of the file complete we can ignore 
                // errors, as Silverlight can deal with it
                progress += file.Size;
                var progress = Math.round(progress * 100 / totalSize);

                if (progress >= totalSize - xapFile.Size) {