A Simple Service

I l @ ve RuBoard

Visual Studio .NET makes it extremely easy to create a Windows service. If you've mastered the primary mouse-button click, you're well on your way to creating your first service. The following example shows part of the code that Visual Studio .NET inserts when you create a new Visual Basic Windows Service project. Here are the steps for creating a bare-bones service:

  1. Derive your class from the System.ServiceProcess.ServiceBase class.

  2. Pass an instance of this derived class to the shared ServiceBase.Run method. (Alternatively, you can pass an array of derived ServiceBase instances to the overloaded ServiceBase.Run method ”multiple services can reside in a single executable file.)

  3. Override the OnStart and OnStop methods .

     ImportsSystem.ServiceProcess PublicClassService1 InheritsSystem.ServiceProcess.ServiceBase <MTAThread()>_ SharedSubMain() DimServicesToRun()AsSystem.ServiceProcess.ServiceBase ServicesToRun=_ NewSystem.ServiceProcess.ServiceBase(){NewService1} System.ServiceProcess.ServiceBase.Run(ServicesToRun) EndSub ProtectedOverridesSubOnStart(ByValargs()AsString) 'Addinitializationcodehere. EndSub ProtectedOverridesSubOnStop() 'Addcleanupcodehere. EndSub EndClass 

The OnStart method is where you should place any initialization code and code that is necessary for starting and running the service. When the ServiceBase.Run method is called, the service is loaded into memory and the constructor is called. When the service is started, the Service Control Manager (SCM) sends a Start command to the service, which in turn invokes the OnStart method in the service. The OnStop method is where you should dispose of any resources your service allocated.

Warning

Do not place any initialization code in the class constructor that must be executed each time the service is started. The class constructor is called only once by the Service Control Manager ”the first time the service is loaded. Subsequent attempts to start the service will most likely fail because this initialization code will not be executed.


Warning

Do not perform a long-running or blocking command in the OnStart method. This method should simply start a timer or another thread and then return immediately; otherwise , the service will fail to start.


ServiceBase also defines several properties and other methods that can be overridden. Tables 7-4 and 7-5 list the properties and the protected methods of the ServiceBase class, respectively.

Table 7-4. ServiceBase Properties

Property

Description

CanStop

Specifies whether the service can be stopped ( True or False ).

CanShutdown

A value of True specifies that the service should be notified when the computer is shutting down.

CanPauseAndContinue

A value of True specifies that the service supports Pause and Continue commands. OnPause and OnContinue base class methods should be overridden when this property is True .

CanHandlePowerEvent

If True , the OnPowerEvent method is triggered when the computer goes into an alternate power state.

AutoLog

If True (the default), status information for the service will automatically be logged when actions occur (such as starting and stopping).

EventLog

An EventLog object with a source already registered with the Application log.

ServiceName

Set or gets the short name of the service.

Table 7-5. ServiceBase Overridable Methods

Method

Description

OnStart

Invoked when the service is started by the SCM or when the operating system boots (if the service is set to run automatically)

OnStop

Invoked when the service is being stopped by the SCM

OnPause

Invoked when a Pause command is sent to the service from the SCM

OnContinue

Invoked when a Continue command is sent to the service from the SCM

OnPowerEvent

Called when the computer's power changes state (to hibernation or system standby mode, for example)

OnShutdown

Invoked before the operation system shuts down

OnCustomCommand

Invoked when a custom command is sent to the service by the SCM

The OnCustomCommand method can be overridden in your derived ServiceBase class. This method takes one parameter ”an integer whose value must be between 0 and 256. Values below 128 correspond to system-reserved values. If the AutoLog property is set to true , custom commands will log an entry to the event log to report the failure or success of the method execution.

One interesting characteristic of services is that they can share a process with other services. Visual Studio .NET is capable of creating two types of services. These correspond to the Win32OwnProcess and Win32ShareProcess ServiceType enumerations. In Visual Studio .NET you cannot create service types that relate to hardware, file system, or other device drivers. With the basics of creating a service covered, we will now move on to creating an actual service.

I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

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