This section briefly discusses the anatomy of a Windows
Service application. The .NET Framework's System.ServiceProcess.ServiceBase
encapsulates the entire functionality that is required to create and control
Windows Service applications.
The following two classes are required for implementing a
Windows Service:
·
System.ServiceProcess.ServiceBase
·
System.Configuration.Install.Installer
The ServiceBase class must be inherited by your Service
class to specify your Service and the Installer class should be inherited in
your Installer class to specify the Service installer.
The following are the methods of ServiceBase class.
·
OnStart
·
OnStop
·
OnPause
·
OnContinue
·
OnShutdown
·
OnPowerEvent
·
OnCustomCommand
The following section discusses these methods briefly.
OnStart: This method is fired when
the service is started by the Service Control Manager. It should be noted that
this method is called only once during the life cycle. This method is used to
specify the processing that occurs when a Start command is received.
OnStop: This method is called when
the service is stopped by the Service Control Manager. This is typically used
to specify the processing that occurs when a Stop command is received by the
Service Control Manager.
OnPause: This method is called when
the service is paused and typically contains the processing for pausing the
service.
OnContinue: This method is called
when the service is resumed after being paused. This method typically contains
the necessary processing so as to enable a service to return to normal
functioning after the same was paused earlier.
OnShutDown: This method is called
when the system is being shutdown and contains the necessary processing
indicative of what should happen prior to the system shutting down.
OnPowerEvent: This method is used to
specify the necessary processing that should take place when the power status
changes.
OnCustomCommand: This method is used
to specify a custom command, i.e., any command other than those discussed
above.
In addition to the above methods, the
System.ServiceProcess.ServiceBase class also contains the following properties:
·
AutoLog
·
CanPauseAndContinue
·
CanShutdown
·
CanStop
·
ServiceName
The following section discusses these properties briefly.
CanStop: This is a boolean property
that is true if the service can be stopped, false otherwise.
CanShutdown: This is a boolean
property that is true if the service wants to be notified that the system on
which it is being executed is being shutdown, false otherwise.
CanPauseAndContinue: This is a
boolean property that is true if the service can be paused and then restarted,
false otherwise.
CanHandlePowerEvent: This is a
boolean property that is set to true if the service should be notified of a
change in the power status of the system on which the service is being
executed.
AutoLog: This is a boolean property
that is set to true if the service should write events to the Application Event
Log when any action is performed.