This documentation is archived and is not being maintained.

ServiceBase Constructor

Creates a new instance of the ServiceBase class.

Namespace: System.ServiceProcess
Assembly: System.ServiceProcess (in system.serviceprocess.dll)

public ServiceBase ()
public ServiceBase ()
public function ServiceBase ()

Do not create an instance of the ServiceBase class. Instead, derive from ServiceBase and instantiate your derived class. The minimum you need to implement in the constructor for a class inherited from ServiceBase is to set the ServiceName on your component. No other processing is specifically required in the constructor. You should handle most initialization in OnStart rather than in the constructor. Otherwise, there is no guarantee the objects will be reinitialized when you restart a service after it has been stopped.

If you override the constructor in your derived class, call the base class constructor in your code.

The ServiceBase constructor sets AutoLog to true. If you do not want to automatically log calls to the service from the Service Control Manager (SCM), set AutoLog to false.

If the executable contains a single service, the system calls the service's constructor when Start is selected from the SCM, and runs the destructor if Stop is called.

If the executable contains multiple services, calling Start on one service causes the constructors to be called for all services in the executable, but only the specified service is started. Destructors for the services are run together when all services have been stopped, not individually when each service is stopped.

NoteNote

If you override the base class constructor, you should explicitly call it in the constructor of your derived class.

The following example shows a constructor for a service class derived from ServiceBase. This code example is part of a larger example provided for the ServiceBase class.

public SimpleService()
{
    CanPauseAndContinue = true;
    CanHandleSessionChangeEvent = true;
    ServiceName = "SimpleService";
}

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: