Jan. 9, 2008, 9:26 a.m.
posted by fractal
Creating Installation ComponentsWhen you deploy an application, you may want to take certain customized actions during the installation process. Some examples of customized actions include registering a Windows service with the Service Control Manager, registering a serviced component with COM+ Explorer, creating a database, installing event logs, installing performance counters, and so on. The .NET Framework provides you with an Installer class to help you perform customized installation actions such as those mentioned above. The Installer class is defined in the System.Configuration.Install namespace. Understanding the Installer ClassThe System.Configuration.Install.Installer class works as a base class for all the custom installers in the .NET Framework. Some of the important members of the Installer class that I will discuss in this chapter are listed in Figure.
You can derive a class from the Installer class and override the methods given in Figure to perform any custom actions. If you want the derived installer class to execute when an assembly is installed, whether by using setup projects or by using the Installer Tool (installutil.exe), then you need to decorate the class with the RunInstaller attribute set to true: [RunInstaller(true)] The installer classes provide the infrastructure for making installation a transactional process. If an error is encountered during the Install() method, the Rollback() method tracks back all the changes and undoes them to leave the machine in the same clean state as it was before the process of installation started. The Rollback() method must know the order in which the installation steps were performed and exactly what changes were made so that it can undo those changes in the proper order. Similarly, when an application is uninstalled, the Uninstall() method of the Installer class is called. The responsibility of the Uninstall() method is again to undo the changes done by the installation process so that the machine is left as clean as if the program had never been installed on it. A question arises: How does the Install() method communicate its installation information to the Rollback() and Uninstall() methods? This question becomes even more interesting when you see that the Install() and Uninstall() methods are not called in the same process. Install() is called when the application is installed, whereas Uninstall() is called when the application is uninstalled. These two events might be separated by several days and computer restarts. The Install() method communicates the installation state by persisting it in a file with the extension .InstallState. This file is placed in the installation directory of the application. The Installer class makes this file available to each of the Install(), Commit(), Rollback(), and Uninstall() methods by passing an IDictionary object to the contents of this file. Content in the .InstallState file is used by the Rollback() and Uninstall() methods to perform the required cleanup operation. Predefined Installation ComponentsThe .NET Framework provides five predefined installation components. Each of these installation components as listed in Figure is associated with a specific server component. Visual Studio .NET does not automatically add an installer for a server component, but it's easy to add one. For example, if you create a new Windows service project and access the Properties window for the project, you see a link as shown in Figure to add predefined installer classes for the Windows service in the project. 28. You can add a predefined installer for a server component by clicking on the Add Installer hyperlink in the Properties window.
Clicking on the Add Installer link adds an instance of the predefined installation component to a class named ProjectInstaller in your project. You can add as many instances of the installation components to the ProjectInstaller class as you need. Each instance of a predefined installation component is set with properties that help in re-creating that object on the production machine. All the installation components are added to the Installers collection of the ProjectInstaller class. When you compile the project to build an EXE or a DLL file, the ProjectInstaller class is now part of the output assembly. The ProjectInstaller class has the RunInstaller attribute set to true, so at the time of installation, the ProjectInstaller class takes the responsibility of installing all the predefined installation components in an assembly. You'll learn how to use predefined installation components to install a Windows service in a forthcoming section, "Deploying a Windows Service." Custom Installation ComponentsYou can also add a custom installer class to a project to perform custom actions during installation, such as compiling the code to a native image or creating a database on a target computer. These installer classes are compiled with your project and are then added to the deployment project as custom actions that are run at the end of the installation. You need to do the following to create a custom installer class:
You'll learn how to use the preceding steps to create a custom installer class in a forthcoming section, "Deploying a Serviced Component." Deploying an Assembly Containing the Installation ComponentsYou can deploy an assembly containing installation components in one of the following two ways:
EXAM TIP Installer Tool (installutil.exe) Performs Installations in a Transactional Manner If you are installing components from multiple assemblies by using the installutil.exe command, be aware that if any assembly fails to install, installutil.exe rolls back the installations of all other assemblies. However, the process of uninstallation is not transactional. I already discussed using the Installer tool (installutil.exe) in Chapter 6, "Windows Services." In the following section, I'll use Visual Studio .NET setup and deployment projects to install the installation components. |
- Comment
