17.6 Create a Windows Service Installer


Problem

You have created a Windows service application and need to install it.

Solution

Extend the System.Configuration.Install.Installer class to create an installer class that contains the information necessary to install and configure your service class. Use the Installer tool (Installutil.exe) to perform the installation.

Discussion

As recipe 17.5 points out, you can't run service classes directly. The high level of integration with the Windows operating system and the information stored about the service in the Windows registry means that services require explicit installation.

If you have Microsoft Visual Studio .NET, you can create an installation component for your service automatically by right-clicking in the design view of your service class and selecting Add Installer from the context menu. This installation component can be called by deployment projects or the Installer tool to install your service. You can also create installer components for Windows services manually by following these steps:

  1. Create a class derived from the Installer class.

  2. Apply the attribute System.ComponentModel.RunInstallerAttribute(true) to the installer class.

  3. In the constructor of the installer class, create a single instance of the System.ServiceProcess.ServiceProcessInstaller class. Set the Account , User , and Password properties of the ServiceProcessInstaller to configure the account under which your service will run.

  4. In the constructor of the installer class, create one instance of the System.ServiceProcess.ServiceInstaller class for each individual service you want to install. Use the properties of the ServiceInstaller objects to configure information about each service, including the following:

    • ServiceName , which specifies the name that Windows uses internally to identify the service. This must be the same as the value assigned to the ServiceBase.ServiceName property.

    • DisplayName , which provides a user-friendly name for the service.

    • StartType , which uses values of the System.ServiceProcess.ServiceStartMode enumeration to control whether the service is started automatically or manually or is disabled.

    • ServiceDependsUpon , which allows you to provide a string array containing a set of service names that must be started before this service can start.

  5. Add the ServiceProcessInstaller object and all ServiceInstaller objects to the System.Configuration.Install.InstallerCollection object accessed through the Installers property, which is inherited by your installer class from the Installer base class.

The ServiceInstallerExample class shown here is an installer for the ServiceExample service created in recipe 17.5. The sample code project for this recipe includes both the ServiceExample and ServiceInstallerExample classes and produces a file named ServiceInstallerExample.exe.

 using System.ServiceProcess; using System.Configuration.Install; using System.ComponentModel; [RunInstaller(true)] public class ServiceInstallerExample : Installer {     public ServiceInstallerExample() {         // Instantiate and configure a ServiceProcessInstaller.         ServiceProcessInstaller ServiceExampleProcess =              new ServiceProcessInstaller();         ServiceExampleProcess.Account = ServiceAccount.LocalSystem;         // Instantiate and configure a ServiceInstaller.         ServiceInstaller ServiceExampleInstaller =              new ServiceInstaller();         ServiceExampleInstaller.DisplayName =              "C# Programmers Cookbook Service Example";         ServiceExampleInstaller.ServiceName = "ServiceExample";         ServiceExampleInstaller.StartType = ServiceStartMode.Automatic;         // Add both the ServiceProcessInstaller and ServiceInstaller to          // the Installers collection, which is inherited from the          // Installer base class.         Installers.Add(ServiceExampleInstaller);         Installers.Add(ServiceExampleProcess);     } } 

To install the ServiceExample service, build the project, navigate to the directory where the ServiceInstallerExample.exe is located (bin\debug by default), and execute the command Installutil ServiceInstallerExample.exe . You can then see and control the ServiceExample service using the Windows Computer Management console. However, despite specifying a StartType of Automatic , the service is initially installed unstarted; you must start the service manually (or restart your computer) before the service will write entries to the event log. Once the service is running, you can view the entries it writes to the Application event log using the Event Viewer application. To uninstall the ServiceExample service, add the /u switch to the Installutil command Installutil /u ServiceInstallerExample.exe .




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net