I am constantly working with Silverlight applications and posting releases regularly. As a rule, I use the
MVVM pattern and its implementation of
Prism . As a result, several XAP files are created that contain application assemblies and a manifest.
Everyone who works according to this approach notices that most XAP files contain duplicate assemblies. For example, when using the
Prism library, almost every XAP file will contain all the assemblies from this library.
Prism adds about 300 KB to each XAP file, which can increase the size of the application by more than 1 MB (if there are 4-5 XAP files). In addition, additional libraries (primarily UI elements) can further increase the size of the application.
All these facts made me start searching for ways to reduce the size of XAP files.
Idea
I was working on one problem when I came across a
note on the Jeff Prosise blog . He mentioned that the assembly could be added to the application, but not to the XAP file. To do this, just set the parameter
')
CopyLocal=false
for each required build that is in the
References list. In this case, the project will link to the desired assembly, but will not add it to the
bin folder during compilation.
This prompted me to the idea that all duplicate assemblies in the application can be stamped
CopyLocal=false
without changing only the build parameters of the main XAP file. It is the main XAP file that should contain all duplicate assemblies that will be automatically loaded into the
Application Domain , and will be available for all assemblies from the remaining XAP files.

The picture above demonstrates how the solution will be updated if it is changed in accordance with the rules described above. Imagine that each project is a separate XAP file. In this case,
Project 1 and Project 2 contain the same pair of assemblies (
Assembly 1 and Assembly 2 ), and I set
CopyLocal=false
for them to exclude them from Project1.xap and Project2.xap.
Assembly 1 (
Assembly 1 ) is already in the main project (
Main Project ), and therefore it is not required to add it to the main project. In contrast,
Assembly 2 should be added to the main project to ensure the operability of the first and second projects.
Project 2 (
Project 2 ) contains assembly 4 (
Assembly 4 ), which is already present in the main project - we exclude this assembly from project 2.
Project 3 (
Project 3 ) does not change, because it contains only unique assemblies.
Let's summarize what has been optimized:
Project | Number of builds in XAP before optimization | Number of builds in XAP after optimization |
Project 1 | four | 2 |
Project 2 | four | one |
Project 3 | 3 | 3 |
Main Project | 3 | four |
Total | 14 | ten |
As you can see, the total number of files in the application has decreased, even despite the increase in the main project by one assembly.
Naturally, making such changes manually will be a slow process and can lead to errors. So I decided that I should write an add-on to Visual Studio that would automate these operations.
Implementing Supplement
I will describe only the general algorithm, according to which the XAP files are optimized.

- Getting all Silverlight projects . The search is based on the fact that the project file contains the parameter
ProjectTypeGuids
It contains a GUID that indicates the type of project (console application, asp.net application, etc.) - Search for the main project . With this project, the launch of the application begins and it should add to it duplicate assemblies from other projects. Search based on project parameter value
SilverlightAppEntry
If this parameter is initialized with the name of a valid (present in the project) class, then such a project is the main one in the application.
- Getting a list of duplicate assemblies. All Silverlight application projects are bypassed and all assemblies that occur more than twice are remembered.
- Adding an assembly duplicate to the main project if it is not there.
- Set the parameter for the assembly
CopyLocal=False
in all projects where it meets, except the main project.
As a result of this algorithm, the current application in Visual Studio will be updated. It can be immediately compiled and see the difference in the size of XAP files.
Visualization of work
Visual Studio allows you to use various built-in mechanisms to display the current state of the executed add-on.
I chose four options:
- Animation . The status bar displays an animated icon throughout the entire process of the add-on.
- Progress bar . The status bar shows the percentage of optimization for each duplicate build.
- Text message The status bar displays a text message about the currently running increment step.
- Logging actions to OutputWindow . All messages are displayed in a separate window during the add-on.
Posting supplement
In order to publish the add-on to the
Visual Studio Gallery , it is enough to have a
Live account, go to the gallery site, click
Upload and follow the add-on publishing wizard.
Add-on installation
The fastest and most convenient way to install an add-on is to use
Visual Studio Gallery . In Visual Studio 2010, it’s enough to open the
Visual Studio Extension Manager (main menu-Tools-Extension Manager), select Online Gallery, and enter “xap” in the Search Online Gallery field. After that, you can select Xaps Minifier and click Download - the download and installation of the add-on will begin.

Use add-ons
After installing and restarting Visual Studio, you should load the Silverlight project, call the context menu for the solution and select Minify Xaps.

The process of analyzing and optimizing the application will start, and progress will be displayed in the status bar of Visual Studio. After the optimization process is completed, a dialog box will appear with information on how many assemblies have been eliminated from the application, how many assemblies the main project has increased, and how many projects have been modified.
Optimization of real projects
The first test program was my demo application, which
I presented at the Remix10 conference. This application contained 5 Silverlight projects and 4 XAP files. The size of the non-optimized application was 1.2Mb, and after optimization it decreased to 500 Kb. At the same time, the size of the main project increased from 120 Kb to 400 Kb.
The second application was the
Prism Demo application , presented by
John Papa at the
PDC09 conference. This application contained 7 Silverlight projects and 4 XAP files, and the total size of XAP files was 5.7 MB. After optimization, this application has decreased to
1.6 MB (!!!).
Known analogues
I am not aware of direct analogues of my implementation of the add-on to Visual Studio. But the theme of optimizing the size of Silverlight applications is relevant, and therefore companies such as
Component One and
Telerik offer their tools. Unlike my idea, they use a completely different approach. Their tools analyze the finished XAP file, find unused code (classes, XAML code) and remove it from the assemblies.
In my opinion, this is to some extent a controversial decision. Here are my arguments:
- I can load classes dynamically and this cannot be traced. In this case, I should remember all the classes / controls that should not be deleted. This can lead to a drastic decrease in the stability of the application, an increase in the load on the testing department, etc. My addition does not change any assemblies, and therefore is much safer.
- I do not think that third-party paid library providers will include in license agreements a clause on the consent of modifying their libraries. That is what is happening now with the operation of the Component One tool. It seems to me that it is simply illegal.
- My colleague Alexey and I spent about one person week on this project. The companies mentioned have spent much more time and effort.
- My addition does not depend on the libraries that are used in the application. At the same time, Telerik Assembly Minifier can only optimize Telerik libraries.
- When using my add-on, developers are not required to re-optimize the application when new versions of libraries appear.
In any case, my add-on can be used with the
Component One XapOptimizer and
Telerik Assembly Minifier tools .
Current restrictions
Currently the add-on does not work with:
- Silverlight for Windows Phone 7;
- XNA for Windows Phone 7;
- Silverlight for Symbian;
There is no technical difficulty to add support for these types of projects, which differ only in GUIDs. In the near future it will be implemented.
In addition, the addition does not allow to get rid of assemblies that are added to the XAP file dynamically. This happens when the added assembly depends on other assemblies, and the latter are not explicitly added to the project. In this case, it is recommended to add these assemblies manually to all projects where it falls into the XAP file and re-optimize the application.
The final limitation that follows from the add-on algorithm is that applications consisting of a single XAP file cannot be optimized.
Acknowledgments
I would like to express my gratitude to my friend and colleague Alexei Gladkikh for implementing the prototype algorithm for optimizing XAP files.
Source texts
The latest sources can be downloaded
here . The add-on installation can be downloaded from the
official add- on
page on the Visual Studio Gallery website or installed via the Visual Studio Extension Manager (see above).
Share your experience
It is very important for me to get feedback from real users of the add-on. Wishes that I have already received made it possible to make Minifier XAPs more convenient and faster. Your comments can take it to a new level.
I hope that this tool will be a must have for every Silverlight developer.