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 the Visual Studio installer project type with your version of Visual Studio, make sure you download and install the 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 actions and upgrades of this type, in this article.

Configuring Basic Installer Actions

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

Right-click on SampleInstaller project > Add > Project Output.

Project Output

From the “Add Project Output Group” dialog, select the appropriate project in the 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 the Properties tab seen right side of the project. The below View can be seen,

 SampleInstaller

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

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

Version number

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

Configuring Custom Actions

Custom Actions are the one that 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.

Custom Actions

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

For instance, by default, the SampleWinService project doesn’t start the installed service on the 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 the SampleWinService project using appropriate classes and methods like the one below.

SampleWinService

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

Upgrade

The upgrade is the process of installing or uninstalling required features components binaries or files from the existing installation. To perform the upgrade using the Visual Studio installer perform these steps.

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

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


Similar Articles