Introduction

In this article we are going to see about how to install a Silverlight OOB application through a setup project. Silverlight OOB applications shortly OOBs are much like desktop applications that can be installed, launched and uninstalled. These OOBs are normally installed from the browser by right clicking the Silverlight control and selecting Install app into this computer, but there are times we have to look into alternate ways of installation for example when we want the OOB app to be easily distributable from machine to machine or to install the OOB as part of batch installation etc. This article helps to understand how to install an OOB application through a setup project.  

I've split the article into below main sections to help readers understand easily.  

  1. A little theory - In this section we are going to see about what is the sllauncher and how we can use it to manually install, uninstall an OOB application from the command prompt.   
  2. Project Plan  - In this section we are going to discuss about our project plan and what are the things need to be configured in the setup project.  
  3. Custom Installer  - In this section we will see about the Installer base class methods that need to be overridden and how to practically invoke the sllauncher for installing or uninstalling a sample application. 
Let's dive into the article! 

A little theory 

a. What is the sllauncher? 

When we install the Silverlight plugin a set of things are downloaded to the machine. If we navigate to the %ProgramFiles%\Microsoft Silverlight\ folder we can see the following. 


Fig a. Microsoft Silverlight Installation Directory 

  • 4.0.51204.0  - this folder contains all the Silverlight framework assemblies.  
  • sllauncher.exe       - the process that will be invoked when we install, launch or uninstall any OOB application. 
The sllauncher.exe is the process that plays crucial role for installing, launching and uninstalling any OOB app. Whenever you launch an OOB application it runs under a separate sllauncher process.

b. sllauncher.exe options

To know more about various options of sllauncher open the command prompt and run, 

"%ProgramFiles%\Microsoft Silverlight\sllauncher.exe"  

We'll see the below error message box,   


Fig b. sllauncher.exe options

That clearly points out we can't use sllaucher.exe directly without passing some parameters and the message box also displays the various options available with it. In the below table I listed down the various options and for what they are used for. 

Option Description
app_id  Whenever an OOB application is installed, the XAP and the associated content are located under a uniquely named folder and the unique name is called as app_id, this parameter is used by sllauncher to located the installed app when we launch it. 
/install: <file path to XAP>  This option is used to install the OOB application passing the physical path of the XAP file ex. /install: D:\SilverlightSamples\SampleOOB.xap 
/emulate: <file path to XAP>  This option is used to launch an OOB application in emulation mode without even installing it. 
/origin: <original app uri>  This option specifies the Uri where the XAP file have come from. This Uri is used for security purposes and auto-updates. For ex. /origin: http://mywebsite.com/SampleOOB.xap 
/overwrite  Overwrite any previously installed version when installing the current version. It's a good practice to use this along with /install switch. 

/shortcut: <desktop|startmenu |desktop+startmenu|none> 

This option specifies the places where the shortcuts has to be created in desktop or startmenu or both or none. 
/debug Including this option launches the application in debug mode. 

Let's assume we have a sample OOB application named SampleOOB.xap located under folder D:\SilverlightSamples\ and the XAP is hosted in http://mywebsite.com/

For installing the OOB application we have to run the following command from the command prompt,

"%ProgramFiles%\Microsoft Silverlight\sllauncher.exe" /install:D:\SilverlightSamples\SampleOOB.xap /shortcut:desktop+startmenu /origin:http://mywebsite.com/SampleOOB.xap /overwrite 

For uninstalling the application we have to run the command,  

"%ProgramFiles%\Microsoft Silverlight\sllauncher.exe" /uninstall /origin:http://mywebsite.com/SampleOOB.xap 

Project Plan

a. Solution  

Our plan is to run the above commands from the installer. When the setup installs we have to install the Silverlight OOB application by launching the sllauncher passing the installation parameters and when uninstalls we have to uninstall our already installed OOB application using the sllauncher passing the necessary parameters. For that, we have to create a custom installer class that is placed in a separate assembly.    

Below is the solution explorer view of the attached source code, 


Fig c. Project Solution Explorer View

Our solution contains four projects. 

  • SlApp - Sample silverlight application. 
  • SlInstaller  - Assembly that contains our custom installer class.
  • SlWeb  - The web application that hosts the XAP component. 
  • SlSetup  - Setup project. 

b. Things to be configured in the setup project

The following are the configurations we have to do in the setup project.

  • Right click the setup project Add -> Project Output  -> Primary output(SlApp). 
  • Right click the setup project Add -> File -> SlApp.xap located in the ClientBin of the web application.   
  • Right click the setup project View -> Custom Actions.   


Fig d. Configuring Custom Actions in Setup Project

Right click each of the custom actions (Install, Commit..) in the left column and select Custom Action -> Application Folder -> Primary output from SlInstaller(Active).  

  • Pass the installation directory path in the CustomActionData property that we'll retrieve in the installer class to locate the SlApp.xap path. To do this right click the Primary output from SlInstaller(Active) node under Install custom action -> Go to Properties -> Enter the CustomActionData as /INSTALLDIR="[TARGETDIR]\".   


Fig e. Passing parameter in CustomActionData

Custom Installer

The Custom Installer project SlInstaller contains the class SlInstaller that extends the built-in Installer class for that we have to add the reference to System.Configuration.Install assembly. [Note: Don't forget to add the RunInstaller attribute over the class].  

/// <summary>
/// Custom Installer Class
/// </summary>
[RunInstaller(true)]
public class SlInstaller : Installer
{

} 

The Installer class contains lot of virtual methods, but we have to override only couple of them OnAfterInstall and Uninstall. You may have a doubt why we are overriding OnAfterInstall instead of Install, we are doing so because for installing the OOB application we need the XAP file(SlApp.xap) and that will be downloaded to the installation directory only after the base installation is done.   

In the OnAfterInstall we are going to run the command to install the OOB application and in the Uninstall method we are going to run the command to uninstall the OOB application. 

/// <summary>
/// Custom Installer Class
/// </summary>
[RunInstaller(true)]
public class SlInstaller : Installer
{
    protected override void OnAfterInstall(IDictionary savedState)
    {
        //Run command to install Silverlight OOB app.
        base.OnAfterInstall(savedState);
    }

    public override void Uninstall(IDictionary savedState)
    {
        //Run command to uninstall Silverlight OOB app.
        base.Uninstall(savedState);
    }
} 

First write the code for installation in the OnAfterInstall method. For installing the OOB app we need the following parameters to form the command-line string.   

  1. sllauncher.exe path 
  2. XAP file's physical path 
  3. XAP file's origination Uri 
Since the Silverlight installation folder varies from machine to machine getting the sllauncher.exe path is little tricky but by using the Environment class we can get the correct path without hard-coding.

string sllauncherPath = string.Format("{0}\\Microsoft Silverlight\\sllauncher.exe", 
                Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)); 

We can get the XAP file's physical path that we passed through the CustomActionData from the Parameters collection in the Context object.  

//getting the installation folder to get the XAP file's physical path.
string installDir = Context.Parameters["INSTALLDIR"];
string xapPath = string.Format("{0}{1}", installDir.Substring(0, installDir.Length - 1), "SlApp.xap");   

The XAP is hosted  in the sample web application(SlWeb) and let's say it is running in port 5555 so the Uri of the XAP originated will be http://localhost:5555/ClientBin/SlApp.xap

public const string originUri = "http://localhost:5555/ClientBin/SlApp.xap";  

Now form the command-line string args and create a new process to invoke sllauncher.exe.   

//form the command-line string
string args = string.Format(@"/install:""{0}"" /origin:""{1}"" /shortcut:""desktop+startmenu"" /overwrite", xapPath, originUri); 

var startInfo = new ProcessStartInfo
{
    CreateNoWindow = true,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    FileName = sllauncherPath,
    Arguments = args
};

using (var process = Process.Start(startInfo))
{
    process.StandardOutput.ReadToEnd();
}  

In the above code we are creating a new ProcessStartInfo instance that contains the sllaucher.exe process path in FileName property and the command-line arguments in Arguments property, then we are creating a new Process instance passing the ProcessStartInfo instance to the constructor. 

That's all the code for installation. The code for uninstallation is much similar as above and it has to be written in the Uninstall method as below. 

public override void Uninstall(IDictionary savedState)
{
    //Uninstall Silverlight OOB app.
    //getting the sllauncher.exe with complete path
    string sllauncherPath = string.Format("{0}\\Microsoft Silverlight\\sllauncher.exe",
        Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));

    string args = string.Format(@"/uninstall /origin:""{0}""", originUri);
            
    var startInfo = new ProcessStartInfo
    {
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        FileName = sllauncherPath,
        Arguments = args
    };

    using (var process = Process.Start(startInfo))
    {
        process.StandardOutput.ReadToEnd();
    }

    base.Uninstall(savedState);
}  

Demo 

I've attached the sample source code that contains all the projects listed in the article. Please go through it and post your valuable comments.   

References

http://timheuer.com/blog/archive/2010/03/25/using-sllauncher-for-silent-install-silverlight-application.aspx 

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"