C H A P T E R 13
■ ■ ■
311
Deployment
Up to now in this book, I have discussed only the process of developing Silverlight applications. In this
chapter, I turn your focus to post development and discuss the topic of deploying your Silverlight
applications.
Deploying Silverlight Applications
Once you have finished developing your Silverlight application, you must then face the question of
deployment. Luckily, Silverlight deployment is a trivial task that really only involves one concept,
XAP files.
XAP Files
When you compile a Silverlight application, the application is packaged into a single file with the
extension .XAP. This file is the only thing that needs to be sent to the client in order to run your
application. The XAP file itself is really nothing special and is nothing more than a zip file with a special
file extension. To prove this, you can simply change the file extension of a XAP file to give it a .ZIP
extension. Once the file has been renamed, you can then view the contents of compressed archive in the
file explorer in Windows or in a zip archive tool such as WinZip.
The reason Silverlight uses XAP files to package applications is really for two benefits. First, by
placing your files in a ZIP archive file, your files are compressed when they are deployed and sent to the
client, which in turn reduces download times and improves the end user experience. Secondly, by
placing your entire Silverlight application in one file, it makes the process of deploying your application
extremely simple.
Hosting Silverlight Content
In order to host Silverlight content on your web server, it is not necessary to be running a Windows
server. In fact, just about any web server can serve Silverlight content, as long as they are set to serve up
XAP files. In IIS7 this is setup by default, so if you are running Windows 2008 Server then your web server
is preconfigured ready to host your Silverlight content.
If you are running a version of IIS previous to IIS7 or if you are running on a non-Windows server,
you must do some minor configuration to enable the MIME types for the Silverlight extensions. The two
MIME types you need to add are in Table 13-1.
CHAPTER 13 ■ DEPLOYMENT
312
Table 13-1. NEED TABLE CAPTION.
Extension MIME Type
.xaml application/xaml+xml
.xap application/x-silverlight-app
Since there are so many different servers out there, I won’t attempt to show you how to setup this MIME
type for each server possibility, so you will need to do some quick research on how to setup MIME types,
though it is an extremely common task for server administration.
Assembly Caching
As the previous section described, when you deploy your Silverlight applications, all files for your application
are included in a XAP package. This includes any assemblies that are required by your application. For
example, your XAP file may look like Figure 13-1, where you can see that a number of assemblies are
included in the package like System.Windows.Controls.Data.dll. This assembly alone is 128KB in size, and
this amount has to be downloaded to each and every client that runs your application. Furthermore, if there
are multiple Silverlight applications that all require the use of the System.Windows.Controls.Data.dll
assembly, each one by default will download their own copy of the assembly.
Figure 13-1. Exploring the contents of a XAP file
CHAPTER 13 ■ DEPLOYMENT
313
Assembly caching is a feature new to Silverlight 3 that allows you to cache assemblies locally and share
them between different Silverlight applications running on a client machine. Let’s run through a very quick
example to show how assembly caching works and how to activate it in your Silverlight applications.
Try It Out: Exploring Assembly Caching
In this exercise, you will create a simple Silverlight application that includes a number of assemblies. You
will then look at the packaged XAP file before and after you activate assembly caching for the application.
You will also explore the source changes that take place when using assembly caching. Let’s get started!
1. In Visual Studio 2008, create a new Silverlight Application named
Ch13_AssemblyCaching and allow Visual Studio to create a Web Application
project to host your application.
2. In MainPage.xaml, make certain your cursor is positioned within the root Grid
and double click on the DataGrid from the Toolbox. After adding these items,
your XAML should look like the following.
<Grid x:Name="LayoutRoot">
<data:DataGrid></data:DataGrid>
</Grid>
3. Build the application by selecting Build Build Solution from the main menu.
4. Expand the ClientBin directory within the host web application’s directory
using the Solution Explorer in Visual Studio. There you should find the
Ch13_AssemblyCaching.xap file as shown in Figure 13-2.
Figure 13-2. Locating your Application’s XAP File
CHAPTER 13 ■ DEPLOYMENT
314
5. Change the filename of this file to be Ch13_AssemblyCaching.xap.zip in order
to explore the contents. Once the file is renamed, open the compressed file in
Windows Explorer. You will see the contents as shown in Figure 13-1. You will
see that there many assemblies contained in the xap file.
6. From Visual Studio right click on the Silverlight application in the Solution
Explorer and select Properties. On the properties dialog, you will see a
checkbox labeled “Reduce XAP size by using library caching.” Check this
option as shown in Figure 13-3 and save your changes.
Figure 13-3. Enabing assembly caching
7. Rebuild the application and then navigate back to the ClientBin directory.
Once again, rename the Ch13_AssemblyCaching.xap file to a *.zip file and
open it in windows explorer. You will see that there are significantly fewer
assemblies contained within the package, as shown in Figure 13-4.
CHAPTER 13 ■ DEPLOYMENT
315
Figure 13-4. XAP File with Assembly Caching Enabled
8. If you then refresh the Solution Explorer and examine the ClientBin folder you
will see that a number of new zip files have been added, as shown in Figure 13-
5. These zip files contain the assemblies that were removed from the *.xap file.
When your Silverlight application needs the specific assemblies they will
download the assembly via the zip file in the ClientBin.