Windows Services Architecture


Three program types are necessary to operate a Windows Service:

  • A service program

  • A service control program

  • A service configuration program

The service program itself provides the actual functionality you are looking for. With a service control program, it’s possible to send control requests to a service, such as start, stop, pause, and continue. With a service configuration program, a service can be installed; it’s copied to the file system, written into the registry, and configured as a service. Although .NET components can be installed simply with an xcopy, because they don’t need to write information to the registry, installation for services requires registry configuration. A service configuration program can also be used to change the configuration of that service at a later point.

These three ingredients of a Windows Service are discussed in the following subsections.

Service Program

Before looking at the .NET implementation of a service, take a look at it from an independent point of view and discover what the Windows architecture of services looks like and what the inner functionality of a service is.

The service program implements the functionality of the service. It needs three parts:

  • A main function

  • A service-main function

  • A handler

Before discussing these parts, you must be introduced to the Service Control Manager (SCM). The SCM plays an important role for services, sending requests to your service to start and to stop it.

Service Control Manager

The SCM is the part of the operating system that communicates with the service. Figure 22-2 illustrates how this communication works with a Unified Modeling Language (UML) sequence diagram.

image from book
Figure 22-2

Important 

At boot time, each process for which a service is set to start automatically is started, and so the main function of this process is called. The service has the responsibility to register the service-main function for each of its services. The main function is the entry point of the service program, and in this function the entry points for the service-main functions must be registered with the SCM.

Main Function, Service-Main, and Handlers

The main function of the service might register more than one service-main function. The service must register a service-main function for each service it provides. A service program can provide a lot of services in a single program; for example, <windows>\system32\services.exe is the service program that includes Alerter, Application Management, Computer Browser, and DHCP Client, among others items.

The SCM now calls the service-main function for each service that should be started. The service-main function contains the actual functionality of the service. One important task of the service-main function is to register a handler with the SCM.

The handler function is the third part of 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. You get many service control programs with the operating system; one is the MMC 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 22-3.

image from book
Figure 22-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’s 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

You can’t use xcopy installation with services, because services must be configured in the registry. You can set the startup type to automatic, manual, or disabled. You have 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 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.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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