15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

676 ❘ ChaPTer 25 windOws services<br />

A wizard generates the code, but change the filename to QuoteService.cs, the name of the namespace to<br />

Wrox.ProCSharp.WinServices, <strong>and</strong> the class name to QuoteService. The code of the service is discussed<br />

in detail shortly.<br />

The serviceBase Class<br />

The ServiceBase class is the base class for all Windows Services developed with the .<strong>NET</strong> Framework. The<br />

class QuoteService is derived from ServiceBase; this class communicates with the SCM using an<br />

undocumented helper class, System.ServiceProcess.NativeMethods, which is just a wrapper class to the<br />

Win32 API calls. The class is internal, so it cannot be used in your code.<br />

The sequence diagram in Figure 25-7 shows the interaction of the SCM, the class QuoteService, <strong>and</strong> the<br />

classes from the System.ServiceProcess namespace. In the sequence diagram, you can see the lifelines of<br />

objects vertically <strong>and</strong> the communication going on horizontally. The communication is time-ordered from<br />

top to bottom.<br />

SCM QuoteService :ServiceBase :NativeMethods<br />

Main()<br />

Run()<br />

StartServiceCtrlDispatcher()<br />

ServiceMainCallback()<br />

OnStart()<br />

RegisterServiceCtrlH<strong>and</strong>ler(Ex)()<br />

on a<br />

stop<br />

request<br />

for the<br />

service<br />

ServiceComm<strong>and</strong>Callback()<br />

OnStop()<br />

figure 25-7<br />

The SCM starts the process of a service that should be started. At startup, the Main() method is called. In<br />

the Main() method of the sample service, the Run() method of the base class ServiceBase is called. Run()<br />

registers the method ServiceMainCallback() using NativeMethods.StartServiceCtrlDispatcher()<br />

in the SCM <strong>and</strong> writes an entry to the event log.<br />

Next, the SCM calls the registered method ServiceMainCallback() in the service program.<br />

ServiceMainCallback() itself registers the h<strong>and</strong>ler in the SCM using NativeMethods.RegisterService<br />

CtrlH<strong>and</strong>ler[Ex]() <strong>and</strong> sets the status of the service in the SCM. Then the OnStart() method is called. In<br />

OnStart(), you need to implement the startup code. If OnStart() is successful, the string “Service started<br />

successfully” is written to the event log.<br />

The h<strong>and</strong>ler is implemented in the ServiceComm<strong>and</strong>Callback() method. The SCM calls this method when<br />

changes are requested from the service. The ServiceComm<strong>and</strong>Callback() method routes the requests<br />

further to OnPause(), OnContinue(), OnStop(), OnCustomComm<strong>and</strong>(), <strong>and</strong> OnPowerEvent().<br />

Main function<br />

This section looks into the application template–generated main function of the service process. In the main<br />

function, an array of ServiceBase classes, ServicesToRun, is declared. One instance of the QuoteService<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!