How To Perform Custom Actions And Upgrades Using Visual Studio Installer

Visual Studio Installer

Visual Studio provides installer projects in order to make application deployment simple. To have Visual Studio installer project type with your version of Visual Studio, make sure you download and install corresponding extension to your machine.

To install Visual Studio Installer for Visual Studio 2015, see here.

Visual Studio Installer Types

There are five types of installer templates available currently:

  • Setup Project
  • Web Setup Project
  • Merge Module Project
  • Setup Wizard
  • CAB Project

    Visual Studio

As our scope is “Setup Project” type, we will focus on adding custom action and upgrades of this type, in this article.

Configuring Basic Installer Actions

Let’s create a project called SampleInstaller using “Setup Project” template. The ultimate purpose of SampleInstaller project is to deploy binaries built using Windows project. Hence, let’s create windows service project called “SampleWinService”. The generated binaries from SampleWinService project has to be deployed to target machine using SampleInstaller project.

Right click on SampleInstaller project > Add > Project Output…

Visual Studio

From “Add Project Output Group” dialog, select appropriate project in Project field for which you planned to ship binaries. Under the list box of this dialog, select “Primary output” to tell the installer that it has to pick up only DLLs and EXEs from the selected project for deployment and click OK.

Now, highlight SampleInstaller and click on Properties tab seen right side of the project. The below View would be seen,

Visual Studio

Now, fill the fields appropriately as per the description mentioned when you highlight the field. Make sure you select appropriate platform using TargetPlatform. If you want to install this installer on 64 bit machine as 64 bit application then select x64 instead of x86.

UpgradeCode is important GUID (Globally Unique Identifier) which is used to identify the family of products using this number. When the Version number of product get changed, the UpgradeCode won’t change but ProductCode get change. This allows user to perform upgrade operation of the product. Hence, make sure that ProductCode is changed when changing the Version, if you planned to perform upgrade. This can be done by clicking YES to below question when you modify the Version number,

Visual Studio

Needless to say that if you don’t want to perform upgrade, then click No option.

Configuring Custom Actions

Custom Actions are the one which enables the main project’s (SampleWinService) custom code to be executed at corresponding installer actions. To configure that:

Right click on SampleInstaller project > View > Custom Actions

Visual Studio

Here, four basic installer actions appear: Install, Commit, Rollback and Uninstall. If you want to customize any of the installer action, highlight the action > Add Custom Action… and select the project and type of output where the custom action handler code been written.

For instance, by default, SampleWinService project doesn’t start the installed service on target machine. Also, we want to stop the service when the product is about to get uninstalled. If we want to achieve them, we need to write custom code in SampleWinService project using appropriate class and methods like below,

Visual Studio

Override OnAfterInstall method of ProjectInstaller class and perform start operation to enable automatic windows service start after installation. Similarly, override OnBeforeUninstall method to stop the running service at the time of product uninstallation.

Upgrade

Upgrade is the process of installing or uninstalling required feature or component or binaries or file from the existing installation. To perform upgrade using Visual Studio installer perform these steps:

  • Change the Version number of the installer from it’s properties
  • Say YES to the dialog popup so that product code get changed
  • Don’t change the UpgradeCode
  • Change the version number of assembly (EXE/DLL) from previously installed version so that installer could understand what assembly needs to be replaced at upgrade.
  • Write appropriate CustomAction handler in OnBeforeInstall method to remove existing service from panel or uninstall the existing version, if required.
  • Build the installer with modified version number, ProductCode and assembly version and Install

Note

When performing upgrades to remove the existing product automatically, one should override OnBeforeInstall method of Installer class and write the code to remove the product. Refer to OnBeforeInstall method for details.

Up Next
    Ebook Download
    View all
    Learn
    View all