MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
Installing a Windows Service
The .NET Framework allows you to create custom installer components. The custom installer components are the classes that can be used by an installation tool such as installutil.exe 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 in the Windows service database. You'll 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 Installer property. You'll add the instance of ServiceProcessInstaller class and instances of the ServiceInstaller class corresponding to your Windows service application to this collection. You'll also apply the RunInstallerAttribute 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:
-
Looks for an Installer class in the Windows service application that has its RunInstallerAttribute set to true and creates an instance of this class.
-
Gets a collection of custom installer objects from the Installer property and invokes the Install() method on each of these objects to perform the custom installation.
-
Rolls back the installation of all custom installers if any one of them failed.
The ServiceProcessInstaller and the ServiceInstaller Classes
I'll talk more about the installation process and the Install class later in Chapter 10. In this section, I'll focus on some important properties of the ServiceProcessInstaller and the ServiceInstaller classes because these properties specify the values, which are written to the Windows service database.
The ServiceProcessInstaller class specifies the security settings for the Windows services in the Windows service application. Table 6.3 lists some important properties of the ServiceProcessInstaller class.
Table 6.3. Important Members 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 property, you are prompted to enter both username and password at the time of installation. |
Password | This property is only useful when the Account property is set to User . In that case, this property specifies the password of the account under which the service application runs. |
Username | This property is only useful when the Account property is set to User . In that case, this 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 6.4 lists some important properties of the ServiceInstaller class.
Table 6.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, and cannot contain forward slash, backslash, or control characters . |
ServicesDependedOn | Specifies an array of strings representing 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. |
EXAM TIP
The ServiceName Property The ServiceName property of a ServiceInstaller object must exactly match the ServiceName property of the corresponding Windows service class.
NOTE
Windows Service Description Unfortunately, the Installer class does not have a property to set the description for a Windows Service. If you need to set a description for a service, you have two choiceseither use the Service Control utility (sc.exe) or modify the registry key corresponding to the Windows service, add a string value named Description , and then set the data for this value with the description text.
Adding Installer Classes to a Windows Service Project
When you use Visual Studio .NET to create a Windows service application, it is easy to create the required installer classes. The Properties window of the Windows service, as shown in Figure 6.4, displays a link called Add Installer. When you click on that link, Visual Studio .NET automatically 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 ProcessInstaller class (depending on the number of Windows services you have in the application).
Step By Step 6.2 demonstrates how to add Installer classes to a Windows service project.
STEP BY STEP
6.2 Creating an Installer for the Windows Service
|
At this stage, the OrderService application has both a Windows service and an installer.
Using installutil.exe to Install a Windows Service Application
An executable file that has the code for the Installer class can be easily installed using the command-line installutil.exe tool that comes as a part of .NET Framework SDK.
Step By Step 6.3 demonstrates how to use installutil.exe to install a Windows service application in the Windows service database.
STEP BY STEP
6.3 Using installutil.exe to Install a Windows Service Application
|
The Windows service application is now stored in the Windows service database. Not only this, but based on the information in the executable file, installutil.exe has also installed an event source named OrderService for writing to the Application event log.
Because the StartType of the OrderService Windows service is set as Automatic, it will start automatically the next time Windows starts.
Starting and Testing a Windows Service
To see the OrderService Windows service in action, you need to first create the directory c:\orders because OrderService depends on it. In most cases, it is helpful to create any such required files or directories at the time of installation itself. However, for that you have to install the Windows service using a Setup package based on the Windows Installer. You'll learn how to do that later in Chapter 10.
Step By Step 6.4 shows how to use the OrderService service to automatically add new records in the Orders table of Northwind database whenever an XML file is created in the c:\orders directory.
STEP BY STEP
6.4 Starting a Windows Service
|
In Step By Step 6.4, you see that as the new XML files are created, they are inserted to the Orders table of the Northwind database. After the order record is inserted, the program also moved the XML file to the c:\orders\updated subdirectory. The FileSystemWatcher is configured not to listen to any of the subdirectories inside c:\orders ; therefore, XML files created in c:\orders\updated do not raise any events of interest to the FileSystemWatcher component.
REVIEW BREAK
|
GUIDED PRACTICE EXERCISE 6.1
Recall from Chapter 3, ".NET Remoting," that when you want to remote an object other than using IIS, you have to create a remoting server and manually start the service whenever the computer is restarted. When you implement a remoting server as a Windows service, it is easy to ensure that the remoting server is automatically started with Windows. The objective of this exercise is to create a Windows service, which exposes the DbConnect class of the StepByStep3_10 project as a Singleton server activated object. You'll use a client program such as the one created in Step By Step 3.12 to connect to this service. The server and client should communicate via the TCP channels and the binary formatter. The service should start automatically when Windows is started. The service should allow itself to be stopped but should not support pause and continue operations. How would you create such a Windows service? You should try working through this problem on your own first. If you get stuck, or if you'd like to see one possible solution, follow these steps:
If you have difficulty following this exercise, review the sections "Creating a Windows Service Application" and "Installing a Windows Service" earlier in this chapter. Also review the sections, "Creating a Remotable class" and "Creating a Server-Activated Object" in Chapter 3. Make sure that you also perform Step By Step 6.1 through Step By Step 6.3 and Step By Step 3.9 through Step By Step 3.12. After doing that review, try this exercise again. |
Top |