Installing a Windows Service

Team-Fly    

Developing XML Web Services and Server Components with Visual C#™ .NET and the .NET Framework, Exam Cram™ 2 (Exam 70-320)
By Amit Kalani, Priti Kalani

Table of Contents
Chapter 7.  Windows Services


The .NET Framework enables you to create custom installer components. The custom installer components are the classes that an installation tool, such as installutil.exe, can use to properly install an application.

The following three installation components are used when creating an installer for the Windows service applications:

  • The System.ServiceProcess.ServiceProcessInstaller Class This class creates the records for all the Windows services contained with the application in the Windows service database. You need to have just one instance of the ServiceProcessInstaller class in a Windows service application.

  • The System.ServiceProcess.ServiceInstaller Class This class writes specific information about a Windows service to the Windows service database. You have as many instances of the ServiceInstaller class as the number of services in a Windows service application.

  • The System.Configuration.Install.Installer Class This class works as the entry point for the installation process. The Installer class maintains a collection of custom installer objects, which can be accessed through the Installers property. You should add the instance of the ServiceProcessInstaller class and instances of the ServiceInstaller class of the Windows service application to this collection. You should also apply the RunInstaller attribute to this class and set its value to true.

When you use an installation tool such as installutil.exe to install a Windows service application, the installation tool takes the following steps:

  1. It looks for an installer class in the Windows service application that has its RunInstaller attribute set to true, and it creates an instance of this class.

  2. It gets a collection of custom installer objects from the Installers property and invokes the Install() method on each of these objects to perform the custom installation.

  3. It rolls back the installation of all custom installers if any one of them failed.

The ServiceProcessInstaller and the ServiceInstaller Classes

The ServiceProcessInstaller class specifies the security settings for the Windows services in the Windows service application. Table 7.3 lists some important properties of the ServiceProcessInstaller class.

Table 7.3. Important Properties of the ServiceProcessInstaller Class

Property

Description

Account

Specifies the type of account under which the services run. This property takes one of the values from the ServiceAccount enumeration. These values are LocalService, LocalSystem, NetworkService, and User.

If you set the Account property to User and do not specify a value for either the Username or Password properties, you are prompted to enter both the username and password at the time of installation.

Password

This property is useful only when the Account property is set to User. In this case, the property specifies the password of the account under which the service application runs.

Username

This property is useful only when the Account property is set to User. In this case, the property specifies the username of the account under which the service application runs.

The ServiceInstaller specifies the individual settings for each service in the Windows service database. Table 7.4 lists some important properties of the ServiceInstaller class.

Table 7.4. Important Members of the ServiceInstaller Class

Property

Description

DisplayName

Specifies a descriptive friendly name that identifies the service to the user. This name is often used by interactive tools to display the service to the user.

ServiceName

Specifies a unique name by which the system identifies this service.

The ServiceName must be between 1 and 256 characters. It cannot contain /, \, or any character from the ASCII character set with a value less than decimal value 32.

ServicesDependedOn

Specifies an array of strings that represents the services that must be running for this service to run. If any service in the array is not running, the SCM tries to start that service before starting this service.

StartType

Indicates how and when this service is started. The value of this property is one of the ServiceStartMode enumeration values Automatic, Disabled, or Manual. The value Automatic specifies that the service is started automatically when Windows starts. The default value of this property is Manual, which means that the service will not start automatically with Windows.

graphics/alert_icon.gif

The ServiceName property of a ServiceInstaller object must exactly match the ServiceName property of the corresponding Windows service class.


Adding Installer Classes to a Windows Service Project

Take the following steps to add and customize the installer classes for the WebsiteMonitor Windows service:

  1. Right-click on the designer area of the WebsiteMonitor Windows service and select Add Installer from the shortcut menu. This action adds a class named ProjectInstaller in your project. This class derives from the Installer class and contains an instance of the ServiceProcessInstaller class and one or more instances of the ServiceInstaller class (depending on the number of Windows services you have in the application).

  2. Change the Account property of the serviceProcessInstaller1 component from User to LocalSystem.

  3. Change the DisplayName and ServiceName properties of the serviceInstaller1 component to WebsiteMonitor, and change the StartType property to Automatic.

  4. Build the project. The Windows service is now ready to be installed.

Using the Installer Tool (installutil.exe) to Install a Windows Service Application

You can install any executable file that has the code for the installer classes by using the command-line Installer tool (installutil.exe), which is a part of the .NET Framework SDK. To install the WebsiteMonitor windows service, you need to open the Visual Studio .NET command prompt, change to the directory where the .exe file for the Windows service is located, and issue the following command:

 installutil Example7_1.exe 
Starting and Testing a Windows Service

If the StartType property of a ServiceInstaller class is set to Automatic, the Windows service is automatically started when the computer is restarted. To start the service manually take the following steps:

  1. Right-click on the My Computer icon and select Manage from its shortcut menu. In the Computer Management window, navigate to Services and Applications, Services node. Select the WebsiteMonitor service from the list of services and then select Action, Start.

  2. Right-click on the My Computer icon and select Manage from its shortcut menu. In the Computer Management window, navigate to System Tools, Event Viewer, Application. You see that WebsiteMonitor service has written an informational message about the successful start of the Windows service.

  3. Wait for a couple of minutes, and then refresh the contents of the event log. If the Web site is reachable, you won't see any error message in the event log from the WebsiteMonitor service.

  4. Modify the contents of the Example7_1.exe.config file to provide a nonexistent URL. Restart the Windows service. Wait for a couple of minutes. You'll notice that the WebsiteMonitor service is logging the error messages to the event log.


    Team-Fly    
    Top


    MCAD Developing XML Web Services and Server Components with Visual C#. NET and the. NET Framework Exam Cram 2 (Exam Cram 70-320)
    Managing Globally with Information Technology
    ISBN: 789728974
    EAN: 2147483647
    Year: 2002
    Pages: 179

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