Windows Services Architecture

 
Chapter 22 - Windows Services
bySimon Robinsonet al.
Wrox Press 2002
  

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 we 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. Finally, we need a service configuration program. 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. While .NET components can be installed simply with an xcopy because they don't need the use of the Registry, installation for services does require Registry configuration. A service configuration program can also be used to change the configuration of that service at a later point.

We shall now look at these three ingredients of a Windows Service.

Service Program

Before looking at the .NET implementation of a service, let's look at it from an independent viewpoint 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 we discuss these parts, we first need to introduce the Service Control Manager ( SCM ). The SCM plays a very important role for services, sending requests to our service to start and stop it.

Service Control Manager (SCM)

The Service Control Manager is the part of the operating system that communicates with the service. Let's have a look at how this communication works with a UML sequence diagram:

click to expand
Important 

At boot time, each process for which a service is set to automatically start is started, and so the main function of this process gets 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 here, the entry points for the service-main functions must be registered with the SCM.

A main function may register more than one service-main function. It 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.

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 the 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. We get many service control programs with the operating system; one is the MMC Services snap-in that we've seen earlier. We can also write our own service control program; a good example of this is the SQL Server Service Manager:

click to expand

Service Control Program

As the name suggests, with a service control program we can control the service. For stopping, suspending, and resuming the service, we 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

We can't use xcopy installation with services, since services must be configured in the Registry. We can set the startup type to automatic, manual, or disabled. We 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#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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