Once we have a Silverlight application ready, we will want to show it to the rest of the world. This means deploying it!
While Silverlight is a .NET technology, it doesn't require .NET to be installed on the server. Remember that it's a client-side technology. The Silverlight plugin on the client will download and run the application using the version of the Common Language Runtime (CLR) embedded in the Silverlight plugin. In this recipe, we'll look at how we can deploy a Silverlight application.
Deploying a Silverlight application is easy; the Silverlight code is compiled and wrapped into a *.xap
file. Getting this file on the client side and running it from there is our only concern. The following steps are to be carried out for deploying a Silverlight application:
1. We'll use the
DeployHelloWorld
application to demonstrate deployment, which is available with the code downloads in theChapter01/DeployHelloWorld
folder. Build the application and notice that Visual Studio has created a*.xap
file in theClientBin
directory. This file, which is nothing more than a*.zip
file but with another extension, contains the assembly (one or more) to which our Silverlight application was compiled, optional resources, and theAppManifest.xaml
file.2. While looking at the files created by default by Visual Studio in the web project, a sample HTML (
DeployHelloWorldTestPage.html
) and ASPX (DeployHelloWorldTestPage.aspx
) page are created for us as shown in the following screenshot:
3. Both pages have an
OBJECT
tag included. One of the parameters is named asSource
and it has a reference to the*.xap
file in theClientBin
as shown in the following code. If we want to deploy the*.xap
file to another location, we need to update this reference. We'll use the default as of now.<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/DeployHelloWorld.xap"/> <param name="onError" value="onSilverlightError" /> <param name="background" value="white" /> <param name="minRuntimeVersion" value="4.0.41108.0" /> <param name="autoUpgrade" value="true" /> <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.4 1108.0" style="text-decoration:none"> <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/> </a> </object>
Note that the value of
minRuntimeVersion
may differ slightly because of different Silverlight version releases.4. If using the HTML page, the following files need to be copied:
DeployHelloWorldTestPage.html
Silverlight.js
ClientBin/DeployHelloWorld.xap
5. If using the ASPX page, we need to copy the following files:
DeployHelloWorldTestPage.aspx
Silverlight.js
ClientBin/DeployHelloWorld.xap
bin
directory, if using code-behind for the ASPX pageweb.config
6. We'll need to test the page in a browser. If it fails to load, check the MIME types served by the web server software. There should be
*.xap
and*.xaml
in there. (They are specified as the data type in theOBJECT
tag.)
One of the best things about Silverlight is that it can run from any type of server. If we're using ASP.NET, PHP, JSP, or plain-old HTML, Silverlight can still be embedded. Silverlight runs on the client side. The plugin has a CLR embedded so that it hosts our application. On the server side, the only thing we need to do is to serve the files (most importantly, the *.xap
file) that will be downloaded to the client side when requested.
If the Silverlight application isn't being shown, it might be that the server software (IIS or Apache) is not configured to serve the file types used by Silverlight (*.xap
' and *.xaml
). Windows Vista SP1 and Windows Server ship with Internet Information Services (IIS 7) while Windows 7 and Windows Server 2008 R2 include IIS 7.5. On these OS versions, both IIS 7 and IIS 7.5 are configured out of the box to serve *.xap
and *xaml
files. On Windows Vista without SP1, we need to add these to the known MIME types. We can do this by opening Internet Information Services (IIS) Manager and selecting MIME Types. Then, we simply click on Add and add the following two items:
.xap
in the File name extension: field andapplication/x-silverlight-app
in the MIME type: field.xaml
in the File name extension: field andapplication/xaml+xml
in the MIME type: field
If the server environment doesn't allow adding MIME types (a shared hosting plan), there's no reason to panic. As a *.xap
file is nothing more than a *.zip
file but with another extension, Silverlight supports the *.xap
file being deployed as a *.zip
file.
To get things working, start by renaming the *.xap
file in the ClientBin
to *.zip
. Also, replace the reference to the *.xap
file to the new name as shown in the following code:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/DeployHelloWorld.zip"/> ... </object>