Saturday, March 12, 2011

Silverlight: Manage XAP Files

The XAP file size for Silverlight projects can become quite large if they are not managed properly. This will cause the initial loading time to increase and slow performance of your application.

Manage common assemblies

To address the duplication of shared assemblies in multiple XAP files, we have couple of options.
  1. The first is to reference all the shared assemblies in the shell application XAP file. Then we can go to the reference of these shared assemblies in each of the modules and set the Copy Local property on the reference to false. This causes the referenced shared assemblies to not be included in the compiled XAP file for that module. However, at runtime, the one provided by the shell application XAP file will also be available to the modules.

    • All the common assemblies are downloaded once.
    • XAP file size for other modules decreases.
    • Subsequent requests are faster.

    • The initial download size will be large.
  2. The second approach is to use application library caching in Silverlight. To use this feature, we need to go to the properties of all the Silverlight projects and select the check box labeled Reduce XAP size by using application library caching. When we do this, any referenced assemblies that have the right metadata files collocated with them will not be included in the XAP file. Instead, they will be placed in a separate .zip file, and the .zip file will be referenced by the XAP file’s ApplicationManifest.xaml file as an external part. These files are added to the browser cache so that they can be reused on subsequent visits. All downloaded files are subject to the caching configuration settings on the server and in the browser. In a typical configuration, files are downloaded only if they are not in the cache or if they are newer than the cached versions. Libraries from Prism, Silverlight SDK and Toolkit have the required metadata files to use this feature.

    If you want to leverage of the functionality for our assemblies, we need to provide the proper metadata files. More about this has been discussed in the following article: How to: Use Application Library Caching

    You can test this option with your application using Fiddler, and see that shared assemblies are only downloaded when they are required and are not downloaded on subsequent requests.

    • XAP file size of all the module decreases.
    • Common assemblies are downloaded separately as they are required.
    • Downloaded common assemblies are cached and available to all modules until new versions are provided. (subject to the caching configuration settings on the server and in the client browser)

    • We cannot use application library caching and out-of-browser support in the same application. Out-of-browser applications require all startup assemblies to reside in the application package.
    • We need to ensure the clients are not using old cached assemblies when new versions of the common assemblies are deployed.

Improve XAP file compression

XAP files are basically just standard ZIP files with different extension, so we can use compression tools (like Winrar) to decrease the file size even more by using the best compression algorithm.

We can create batch file that can be added to all the Silverlight projects to automatically re-compress the project’s XAP file more efficiently. And we can do this easily by adding command in the project’s post-build events. Around 5% to 20% reduction can be achieved depending on the contents of the XAP file.