Export (0) Print
Expand All

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)

public class ServiceBase : Component

The ServiceBase type exposes the following members.

  NameDescription
Public methodServiceBaseCreates a new instance of the ServiceBase class.
Top

  NameDescription
Public propertyAutoLogIndicates whether to report Start, Stop, Pause, and Continue commands in the event log.
Public propertyCanHandlePowerEventGets or sets a value indicating whether the service can handle notifications of computer power status changes.
Public propertyCanHandleSessionChangeEventGets or sets a value that indicates whether the service can handle session change events received from a Terminal Server session.
Public propertyCanPauseAndContinueGets or sets a value indicating whether the service can be paused and resumed.
Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event. (Inherited from Component.)
Public propertyCanShutdownGets or sets a value indicating whether the service should be notified when the system is shutting down.
Public propertyCanStopGets or sets a value indicating whether the service can be stopped once it has started.
Public propertyContainerGets the IContainer that contains the Component. (Inherited from Component.)
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Public propertyEventLogGets an event log you can use to write notification of service command calls, such as Start and Stop, to the Application event log.
Protected propertyEventsGets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public propertyExitCodeGets or sets the exit code for the service.
Protected propertyServiceHandleGets the service control handle for the service.
Public propertyServiceNameGets or sets the short name used to identify the service to the system.
Public propertySiteGets or sets the ISite of the Component. (Inherited from Component.)
Top

  NameDescription
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the Component. (Inherited from Component.)
Protected methodDispose(Boolean)Disposes of the resources (other than memory) used by the ServiceBase. (Overrides Component.Dispose(Boolean).)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected methodOnContinueWhen implemented in a derived class, OnContinue runs when a Continue command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service resumes normal functioning after being paused.
Protected methodOnCustomCommandWhen implemented in a derived class, OnCustomCommand executes when the Service Control Manager (SCM) passes a custom command to the service. Specifies actions to take when a command with the specified parameter value occurs.
Protected methodOnPauseWhen implemented in a derived class, executes when a Pause command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service pauses.
Protected methodOnPowerEventWhen implemented in a derived class, executes when the computer's power status has changed. This applies to laptop computers when they go into suspended mode, which is not the same as a system shutdown.
Protected methodOnSessionChangeExecutes when a change event is received from a Terminal Server session.
Protected methodOnShutdownWhen implemented in a derived class, executes when the system is shutting down. Specifies what should occur immediately prior to the system shutting down.
Protected methodOnStartWhen implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.
Protected methodOnStopWhen implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
Public methodRequestAdditionalTimeRequests additional time for a pending operation.
Public methodStatic memberRun(ServiceBase)Registers the executable for a service with the Service Control Manager (SCM).
Public methodStatic memberRun(ServiceBase[])Registers the executable for multiple services with the Service Control Manager (SCM).
Public methodServiceMainCallbackInfrastructure. Registers the command handler and starts the service.
Public methodStopStops the executing service.
Public methodToStringReturns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Top

  NameDescription
Public eventDisposedOccurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Top

  NameDescription
Public fieldStatic memberMaxNameLengthIndicates the maximum size for a service name.
Top

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.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft