Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Installing Silverlight OOB Application using a Setup Project

Rate me:
Please Sign up or sign in to vote.
4.78/5 (7 votes)
11 Apr 2011CPOL6 min read 59K   1.1K   20   14
This article demonstrates how we can install a Silverlight OOB application using a setup project.

Introduction

In this article, we are going to see about how to install a Silverlight OOB application through a setup project. Silverlight OOB applications, in short 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 the following 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 that 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:

Image 1

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 a 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 error message box shown below:

Image 2

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 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, example: /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 example: /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 have 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 it 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:

Image 3

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.

Image 4
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]\".

Image 5

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].

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

} 

The Installer class contains a 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.

C#
/// <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 a little tricky but by using the Environment class, we can get the correct path without hard-coding.

C#
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.

C#
//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.

C#
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.

C#
//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 very similar to the above and it has to be written in the Uninstall method as below:

C#
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.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Trigent Software Private Limited
India India
I'm a software developer from south tip of India. I spent most of the time in learning new technologies. I've a keen interest in client-side technologies especially JavaScript and admire it is the most beautiful language ever seen.

I like sharing my knowledge and written some non-popular articles. I believe in quality and standards but blames myself for lagging them.

I believe in small things and they makes me happy!

Comments and Discussions

 
QuestionVery frustrating issue with Sliverlight - sllauncher.exe, version: 5.1.40416.0 Pin
mwwilliams2-Sep-15 3:46
mwwilliams2-Sep-15 3:46 
QuestionHow to make the installer elevated trust? Pin
Ajin Prasad21-Aug-13 22:50
Ajin Prasad21-Aug-13 22:50 
QuestionError Pin
Morteza Giti23-Jan-13 21:27
Morteza Giti23-Jan-13 21:27 
GeneralMy vote of 5 Pin
Jalal Khordadi29-Dec-11 22:31
Jalal Khordadi29-Dec-11 22:31 
GeneralRe: My vote of 5 Pin
After20505-Feb-12 23:42
After20505-Feb-12 23:42 
GeneralMy vote of 5 Pin
Winner7827-Sep-11 21:33
Winner7827-Sep-11 21:33 
GeneralRe: My vote of 5 Pin
After205030-Sep-11 4:05
After205030-Sep-11 4:05 
QuestionName of the project Pin
Fred_Som6-Sep-11 21:30
Fred_Som6-Sep-11 21:30 
AnswerRe: Name of the project Pin
After205030-Sep-11 4:05
After205030-Sep-11 4:05 
QuestionInstalling Silverlight OOB Application using a Setup Project Pin
dineshd8722-Jun-11 3:36
dineshd8722-Jun-11 3:36 
AnswerRe: Installing Silverlight OOB Application using a Setup Project [modified] Pin
After205022-Jun-11 4:14
After205022-Jun-11 4:14 
GeneralRe: Installing Silverlight OOB Application using a Setup Project Pin
dineshd8722-Jun-11 20:54
dineshd8722-Jun-11 20:54 
GeneralRe: Installing Silverlight OOB Application using a Setup Project Pin
After205023-Jun-11 2:21
After205023-Jun-11 2:21 
GeneralRe: Installing Silverlight OOB Application using a Setup Project Pin
Winner7828-Sep-11 21:16
Winner7828-Sep-11 21:16 
This link will help you:
http://www.alexjamesbrown.com/uncategorized/deploying-net-4-project-error-1001-system-badimageformatexception/[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.