Service Application Programming Architecture

Note

This article doesn't apply to hosted services in .NET. For the latest content on Windows services using Microsoft.Extensions.Hosting.BackgroundService and the Worker Service template, see:

Windows Service applications are based on a class that inherits from the System.ServiceProcess.ServiceBase class. You override methods from this class and define functionality for them to determine how your service behaves.

The main classes involved in service creation are:

In addition, a class named ServiceController can be used to manipulate the service itself. This class is not involved in the creation of a service, but can be used to start and stop the service, pass commands to it, and return a series of enumerations.

Defining Your Service's Behavior

In your service class, you override base class functions that determine what happens when the state of your service is changed in the Services Control Manager. The ServiceBase class exposes the following methods, which you can override to add custom behavior.

Method Override to
OnStart Indicate what actions should be taken when your service starts running. You must write code in this procedure for your service to perform useful work.
OnPause Indicate what should happen when your service is paused.
OnStop Indicate what should happen when your service stops running.
OnContinue Indicate what should happen when your service resumes normal functioning after being paused.
OnShutdown Indicate what should happen just prior to your system shutting down, if your service is running at that time.
OnCustomCommand Indicate what should happen when your service receives a custom command. For more information on custom commands, see MSDN online.
OnPowerEvent Indicate how the service should respond when a power management event is received, such as a low battery or suspended operation.

Note

These methods represent states that the service moves through in its lifetime; the service transitions from one state to the next. For example, you will never get the service to respond to an OnContinue command before OnStart has been called.

There are several other properties and methods that are of interest. These include:

  • The Run method on the ServiceBase class. This is the main entry point for the service. When you create a service using the Windows Service template, code is inserted in your application's Main method to run the service. This code looks like this:

    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
        { new Service1() };
    Run(ServicesToRun);
    
    Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    ServicesToRun =
        New System.ServiceProcess.ServiceBase() {New Service1()}
    System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    

    Note

    These examples use an array of type ServiceBase, into which each service your application contains can be added, and then all of the services can be run together. If you are only creating a single service, however, you might choose not to use the array and simply declare a new object inheriting from ServiceBase and then run it. For an example, see How to: Write Services Programmatically.

  • A series of properties on the ServiceBase class. These determine what methods can be called on your service. For example, when the CanStop property is set to true, the OnStop method on your service can be called. When the CanPauseAndContinue property is set to true, the OnPause and OnContinue methods can be called. When you set one of these properties to true, you should then override and define processing for the associated methods.

    Note

    Your service must override at least OnStart and OnStop to be useful.

You can also use a component called the ServiceController to communicate with and control the behavior of an existing service.

See also