Windows Services are programs that can be started automatically at boot time without the need for anyone to log on to the machine. In this chapter, you learn:
- The architecture of Windows Services, including the functionality of a service program, a service control program, and a service configuration program.
- How to implement a Widows Service with the classes found in the system. Service Process namespace.
- Installation programs to configure the Windows Service in the registry.
- How to write a program to control the Windows Service using the Service Controller class.
- How to troubleshoot Windows Service programs.
- How to react to power events from the operating system.
The first section explains the architecture of Windows Services. You can download the code for this chapter from the www.csharpaid.com
What Is a Windows Service?
Windows Services are applications that can be automatically started when the operating system boots. They can run without having an interactive user logged on to the system and do some processing in the background. For example, on a Widows Server, system networking services should be accessible from the client without a user logging on to the server. On the client system, services are useful as well; for example, to get a new software version from the Internet or to do some file cleanup on the local disk. You can configure a Windows Service to be run from a specially configured user account or from the system user account a user account that has even more privileges than that of the system administrator. Unless otherwise noted, when we refer to 9 service, we are referring to a Windows Service.
The SCM now cans the service-main function for each service that should be started. One important task of the service-main function is to register a handler with the SCM. The handler function is the third part of a service program. The handler must respond to events from the SCM. Services can be stopped, suspended, and resumed, and the handler must react to these events.Once a handler has been registered with the SCM, the service control program can post requests to the SCM to stop, suspend, and resume the service. The service control program is independent of the SCM and the service itself. The operating system contains many service control programs, for example, the Services snap-in that you saw earlier. You can also write your own service control program; a good example of this is the SQL Server Configuration Manager shown in Figure 23-3.
Service Control Program
As the name suggests, with a service control program, you can control the service. For stopping, suspending, and resuming the service, you can send control codes to the service, and the handler should react to these events. It is also possible to ask the service about the actual status and to implement a custom handler that responds to custom control codes.
Service Configuration Program
Because services must be configured in the registry, you can’t use Copy installation with services. The registry contains the startup Type of the service which can be set to automatic, manual, or disabled. You also need to configure the user of the service program and dependencies of the service – for example, the services that must be started before this one can start. All of these configurations are made within a service configuration program. The installation program can use the service configuration program to configure the service, but this program can also be used at a later time to change service configuration parameters.