Chapter 9. Handling Prerequisites
In this chapter, we will cover the following recipes:
Stopping the installation with a launch condition
Installing only to supported versions of Windows
Redistributing the .NET Framework with a bootstrapper
Executing either a 64-bit or 32-bit MSI depending on the user's operating system
Downloading resources from the Web with a web installer
We have several options when it comes to handling prerequisites needed by our installer. When I say prerequisites, I mean frameworks such as .NET, databases such as SQL Server, and the environment itself such as having the expected version of Windows. We can stop the installation outright if our requirements aren't met. On the other hand, in some cases, we may choose to download the missing items. For example, if the end user is missing the .NET Framework, we can install it prior to installing our own software.
In this chapter, we'll cover several scenarios, including how to prevent installations to the wrong environment, download components from the Web, and install either 32-bit or 64-bit files depending on which architecture the operating system supports.
Stopping the installation with a launch condition
A launch condition checks the user's computer to make sure that it can support our software. If it can't, the installation is automatically stopped before the user can go any further. For example, we may want to prevent the installation if the user doesn't have the necessary version of .NET installed. In this recipe, we'll do just that by adding a condition that checks for .NET Framework 4.5.
Windows 8 comes with .NET 4.5 preinstalled, but Windows 7 doesn't. We can use NetFxExtension
to check whether .NET 4.5 is preinstalled, and if it's missing, a message is displayed telling the user that the installation cannot continue. A nice thing about launch conditions is the ability to show the user a warning message explaining what went wrong.
To get started, perform the following steps:
Create a new setup project and name it LaunchConditionInstaller
.
Add a reference to NetFxExtension
by right-clicking on the References node in Solution...
Installing only to supported versions of Windows
Once we're confident that our application works on a particular version of Windows, we may want to prevent users from installing to other unsupported operating systems. Here, we can use a launch
condition. Launch conditions evaluate the state of the user's system and then cancel the installation if our requirements aren't met. By checking the operating system version, we can save the end user from trying to use our software on a system it was never intended for.
Create a new setup project and name it SupportedWindowsInstaller
.
Add a launch
condition that checks the VersionNT
property to see what the computer's version of Windows is:
Add a Condition
element inside the Product
element. Its Message
attribute will explain that the current operating system is not supported, as follows:
Add a conditional statement inside that element that compares...
Redistributing the .NET Framework with a bootstrapper
A bootstrapper checks for prerequisites and installs them if they're missing. So, we could install .NET or SQL Server if it's not present and then follow up by installing our own software. WiX Toolset adds a Visual Studio project template called Bootstrapper Project. In this recipe, we'll see how to deploy the .NET Framework with it.
Create a new setup project and call it InstallerThatNeedsDotNet
. We'll use a bootstrapper to bundle this MSI with the .NET Framework.
Add a bootstrapper project, reference NetFxExtension
within it, and then include one of its .NET packages in the bootstrapper:
Add a Bootstrapper Project to the solution by right-clicking on the solution in Solution Explorer and going to Add | New Project... | Windows Installer XML | Bootstrapper Project | OK:
NetFxExtension
contains packages to install .NET. Add it to the project by right-clicking on the References node in Solution Explorer and going...
Executing either a 64-bit or 32-bit MSI depending on the user's operating system
An MSI can target a 64-bit or 32-bit processor architecture, but never both. An MSI contains metadata called Template Summary that denotes which architecture it supports, and it can only support one or the other at a time. However, there is a way to give the user a single package that will install either 64-bit or 32-bit software, depending on their operating system. In this recipe, we will build such a package, combining both architectures into a single bootstrapper bundle.
To prepare for this recipe, follow these steps:
Create two new setup projects within the same Visual Studio solution. The first is our 32-bit installer and is called ThirtyTwoBitInstaller
. It targets ProgramFilesFolder
in its directory structure:
Downloading resources from the Web with a web installer
A web installer is a bootstrapper that downloads some or all of its resources from the web during installation. This way, the initial download of the bootstrapper is faster, since its file size is much smaller. The main portion of it is hosted online where it's only downloaded when you need it. The disadvantage is that end users will need an Internet connection when they run the installer. However, in many cases, that's a small price to pay.
In this recipe, we'll need to have our resources hosted online. When I say resources, I'm talking about MSI packages, other executable installers, and patch files. To give you an idea about how it would work, we'll use an MSI file that's already on the Web: the Node.js installer.
To prepare for this recipe, perform the following steps:
Create a new bootstrapper project and call it WebInstaller
.
Download the Node.js MSI from http://nodejs.org/download and copy it to the directory of the...