15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Windows services architecture ❘ 669<br />

The service program implements the functionality of<br />

the service. It needs three parts:<br />

SCM<br />

➤<br />

➤<br />

➤<br />

A main function<br />

A service-main function<br />

A h<strong>and</strong>ler<br />

start service process<br />

Service<br />

Before discussing these parts, we need to quickly<br />

introduce you to the Service Control Manager (SCM).<br />

The SCM plays an important role for services —<br />

sending requests to your service to start <strong>and</strong> to stop it.<br />

register service-mains<br />

service-main<br />

service Control Manager<br />

The SCM is the part of the operating system that<br />

communicates with the service. Figure 25-2 illustrates<br />

how this communication works with a sequence diagram.<br />

At boot time, each process for which a service is set to<br />

figure 25-2<br />

start automatically is started, <strong>and</strong> so the main function of<br />

this process is called. The service has the responsibility<br />

of registering the service-main function for each of its services. The main function is the entry point of the service<br />

program, <strong>and</strong> in this function the entry points for the service-main functions must be registered with the SCM.<br />

Main function, service-Main, <strong>and</strong> H<strong>and</strong>lers<br />

register h<strong>and</strong>ler<br />

The main function of the service is the normal entry point of a program, the Main() method. The main<br />

function of the service might register more than one service-main function. The service-main function<br />

contains the actual functionality of the service. The service must register a service-main function<br />

for each service it provides. A service program can provide a lot of services in a single program; for<br />

example, \system32\services.exe is the service program that includes Alerter, Application<br />

Management, Computer Browser, <strong>and</strong> DHCP Client, among other items.<br />

The SCM now calls the service-main function for each service that should be started. One important task of<br />

the service-main function is to register a h<strong>and</strong>ler with the SCM.<br />

The h<strong>and</strong>ler function is the third part of a service program. The h<strong>and</strong>ler must respond to events from the<br />

SCM. Services can be stopped, suspended, <strong>and</strong> resumed, <strong>and</strong> the h<strong>and</strong>ler must react to these events.<br />

After a h<strong>and</strong>ler has been registered with the SCM, the service control program can post requests to the<br />

SCM to stop, suspend, <strong>and</strong> resume the service. The service control program is independent of the SCM <strong>and</strong><br />

the service itself. The operating system contains many service control programs, for example, the MMC<br />

Services snap-in that you saw earlier. You can also write your own service control program; a good example<br />

of this is the SQL Server Configuration Manager shown in Figure 25-3.<br />

figure 25-3<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!