CHAPTER 5
Once the application executable file is built, it needs to be deployed in the computer where it will work—either a workstation or server. Trying to build an msi installation file could be the most reliable way to do this. But there’s also a simpler way to accomplish the deployment process: creating a .BAT file in which the installutil.exe tool will be used.
The Installer tool is a command-line utility that allows you to install and uninstall server resources by executing the installer components in specified assemblies. This tool works in conjunction with classes in the System.Configuration.Install namespace.
This tool is automatically installed with Visual Studio. To run the tool, use the Developer Command Prompt (or the Visual Studio Command Prompt in Windows 7).
At the command prompt, type the following:
Code Sample 22
installutil [/u[ninstall]] [options] assembly [[options] assembly] ... |
Argument | Description |
Assembly | The file name of the assembly in which to execute the installer components. Omit this parameter if you want to specify the assembly's strong name by using the /AssemblyName option. |
Option | Description |
|---|---|
h[elp] -or- /? | Displays command syntax and options for the tool. |
/help assembly -or- /? assembly | Displays additional options recognized by individual installers within the specified assembly, along with command syntax and options for InstallUtil.exe. This option adds the text returned by each installer component's Installer.HelpText property to the help text of InstallUtil.exe. |
/AssemblyName "assemblyName ,Version=major.minor.build.revision ,Culture=locale ,PublicKeyToken=publicKeyToken" | Specifies the strong name of an assembly, which must be registered in the global assembly cache. The assembly name must be fully qualified with the version, culture, and public key token of the assembly. The fully qualified name must be surrounded by quotes. For example, "myAssembly, Culture=neutral, PublicKeyToken=0038abc9deabfle5, Version=4.0.0.0" is a fully qualified assembly name. |
/InstallStateDir=[ directoryName] | Specifies the directory of the .InstallState file that contains the data used to uninstall the assembly. The default is the directory that contains the assembly. |
/LogFile= [filename] | Specifies the name of the log file where installation progress is recorded. By default, if the /LogFile option is omitted, a log file named assemblyname.InstallLog is created. If filename is omitted, no log file is generated. |
/LogToConsole ={true|false} | If true, displays output to the console. If false (the default), suppresses output to the console. |
/ShowCallStack | Outputs the call stack to the log file if an exception occurs at any point during installation. |
/u [ninstall] | Uninstalls the specified assemblies. Unlike the other options, /u applies to all assemblies regardless of where the option appears on the command line. |
Remarks
.NET Framework applications consist of traditional program files and associated resources, such as message queues, event logs, and performance counters that must be created when the application is deployed. You can use an assembly's installer components to create these resources when your application is installed, and to remove them when your application is uninstalled. Installutil.exe detects and executes these installer components.
You can specify multiple assemblies on the same command line. Any option that occurs before an assembly name applies to that assembly's installation. Except for /u and /AssemblyName, options are cumulative but can be overridden. That is, options specified for one assembly apply to all subsequent assemblies unless the option is specified with a new value.
If you run Installutil.exe against an assembly without specifying any options, it places the following three files into the assembly's directory:
Installutil.exe uses reflection to inspect the specified assemblies and to find all Installer types that have the System.ComponentModel.RunInstallerAttribute attribute set to true. The tool then executes either the Installer.Install or the Installer.Uninstall method on each instance of the Installer type. Installutil.exe performs installation in a transactional manner; that is, if one of the assemblies fails to install, it rolls back the installations of all other assemblies. Uninstall is not transactional.
This file can be created using a text editor like Notepad. Once the file is created, it should look like the following sample.
Code Sample 23
@ECHO OFF CLS ECHO Installing Windows Service INSTALLUTIL.EXE monitorservice.exe ECHO Service has been installed PAUSE |
Likewise, the uninstall process can be performed using a .BAT file, and should look like the following snippet.
Code Sample 24
@ECHO OFF CLS ECHO Uninstalling Windows Service INSTALLUTIL.EXE /U monitorservice.exe ECHO Service has been uninstalled PAUSE |
Both files look almost the same. The only difference is the /U option used for INSTALLUITL.EXE in the uninstall file.
Note: To ensure the service will be properly deployed, Installutil.exe must be included in the service distribution package.
To deploy the service executable, a distribution package is needed. This package will contain the necessary files to make a successful installation in the target computer. The following files must be included in the service distribution package:
Tip: For an easy distribution, the package can be shipped in a zip file, which can be decompressed in the target computer at install time.
An msi file could be a reliable way to deploy the service executable file in the target computer, but using a .BAT command file with the Installer tool is an easy way to do it.
The Installer tool (Installutil.exe) is a command-line utility that allows you to install and uninstall server resources. This tool works in conjunction with the System.Configuration.Install namespace. This tool is automatically installed with Visual Studio.
Installutil.exe uses reflection to inspect the specified assemblies and to find all Installer types that have the System.ComponentModel.RunInstallerAttribute attribute set to true. The tool then executes either the Installer.Install or the Installer.Uninstall method on each instance of the Installer type.
A couple of .BAT files must be included in the distribution package; one for installing the service executable file, and another one to perform uninstallation of the executable from the target computer.