.NET Framework Class Library
ServiceBase Class

Provides a base class for a service that will exist as part of a service application. ServiceBase must be derived from when creating a new service class.

Namespace:  System.ServiceProcess
Assembly:  System.ServiceProcess (in System.ServiceProcess.dll)
Syntax

Visual Basic (Declaration)
Public Class ServiceBase _
    Inherits Component
Visual Basic (Usage)
Dim instance As ServiceBase
C#
public class ServiceBase : Component
Visual C++
public ref class ServiceBase : public Component
JScript
public class ServiceBase extends Component
Remarks

Derive from ServiceBase when defining your service class in a service application. Any useful service overrides the OnStart and OnStop methods. For additional functionality, you can override OnPause and OnContinue with specific behavior in response to changes in the service state.

A service is a long-running executable that does not support a user interface, and which might not run under the logged-on user account. The service can run without any user being logged on to the computer.

By default, services run under the System account, which is not the same as the Administrator account. You cannot change the rights of the System account. Alternatively, you can use a ServiceProcessInstaller to specify a user account under which the service will run.

An executable can contain more than one service but must contain a separate ServiceInstaller for each service. The ServiceInstaller instance registers the service with the system. The installer also associates each service with an event log that you can use to record service commands. The main() function in the executable defines which services should run. The current working directory of the service is the system directory, not the directory in which the executable is located.

When you start a service, the system locates the executable and runs the OnStart method for that service, contained within the executable. However, running the service is not the same as running the executable. The executable only loads the service. The service is accessed (for example, started and stopped) through the Service Control Manager.

The executable calls the ServiceBase derived class's constructor the first time you call Start on the service. The OnStart command-handling method is called immediately after the constructor executes. The constructor is not executed again after the first time the service has been loaded, so it is necessary to separate the processing performed by the constructor from that performed by OnStart. Any resources that can be released by OnStop should be created in OnStart. Creating resources in the constructor prevents them from being created properly if the service is started again after OnStop has released the resources.

The Service Control Manager (SCM) provides a way to interact with the service. You can use the SCM to pass Start, Stop, Pause, Continue, or custom commands into the service. The SCM uses the values of CanStop and CanPauseAndContinue to determine whether the service accepts Stop, Pause, or Continue commands. Stop, Pause, and Continue are enabled in the SCM's context menus only if the corresponding property CanStop or CanPauseAndContinue is true in the service class. If enabled, the command is passed to the service, and OnStop, OnPause, or OnContinue is called. If CanStop, CanShutdown, or CanPauseAndContinue is false, the corresponding command-handling method (such as OnStop) will not be processed, even if you have implemented the method.

You can use the ServiceController class to do programmatically what the SCM does using a user interface. You can automate the tasks available in the console. If CanStop, CanShutdown, or CanPauseAndContinue is true but you have not implemented a corresponding command-handling method (such as OnStop) the system throws an exception and ignores the command.

You do not have to implement OnStart, OnStop, or any other method in ServiceBase. However, the service's behavior is described in OnStart, so at minimum, this member should be overridden. The main() function of the executable registers the service in the executable with the Service Control Manager by calling the Run method. The ServiceName property of the ServiceBase object passed to the Run method must match the ServiceName property of the service installer for that service.

You can use InstallUtil.exe to install services on your system.

NoteNote:

You can specify a log other than the Application event log to receive notification of service calls, but neither the AutoLog nor the EventLog property can write to a custom log. Set AutoLog to false if you do not want to use automatic logging.

Inheritance Hierarchy

System..::.Object
  System..::.MarshalByRefObject
    System.ComponentModel..::.Component
      System.ServiceProcess..::.ServiceBase
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Community Content

MattM1121
The example code produces an access violation - here is the solution.

This code example does not actually work and will result in a access violation (see Event Viewer) when trying to start the service. For the C# example, the definition of the SetServiceStatus( ) method should be:

public static extern bool SetServiceStatus( IntPtr hServiceStatus, ref SERVICE_STATUS lpServiceStatus );

The VB code errors as well; this change will fix it:

Public Declare Auto Function SetServiceStatus Lib "ADVAPI32.DLL" Alias "SetServiceStatus" (ByVal hServiceStatus As IntPtr, ByRef lpServiceStatus As SERVICE_STATUS) As Boolean

Tags : contentbug

Kim Hamilton - MSFT
This code sample will only confuse you

In addition to the problems described in the previous comment, this code sample isn't a representative of a typical managed service.

For example, the calls to SetServiceStatus aren't necessary because ServiceBase will do this for you. You can certainly do this in your service, but it makes no sense.

I'm opening a doc bug on this. Until the code sample gets straightened out, use the code sample from the BCL blog:

Writing a Useful Windows service in .NET in Five Minutes

http://blogs.msdn.com/bclteam/archive/2005/03/15/396428.aspx

Tags : contentbug

Fasty1
ServiceBase Usage Pattern

If you did not read the code, which I typically don't since it is so poorly formated and full of language fluff (ie. {, }, public, void, ...) , one of the key points not mentioned in the remarks is that the ServiceBase usage patterns which is to act as a thread controler. Consequently, the public methods of OnStart, OnStop, etc, should be implemented to control a seperate thread which will/is actually performing the work. In particular, OnStart is start the thread (perhaps creating it if necessary as well), while OnStop stops the thread (and possibly destroy it). The up shot is that you don't code the work of your Service in the OnStart method but rather start a thread (see delegates) on some method of possibly the same object that does the work.

Personally I would have thought that the framework would call OnStart on this seperate dedicated thread so that the OnStart method can be the worker enter point of the service. Oh well.

Tags : contentbug

Page view tracker