How to: Create Windows Services
This documentation isn't for the latest version of Windows Service. For the latest content on Windows Services using BackgroundService and the Worker Service template, see:
When you create a service, you can use a Visual Studio project template called Windows Service. This template automatically does much of the work for you by referencing the appropriate classes and namespaces, setting up the inheritance from the base class for services, and overriding several of the methods you're likely to want to override.
The Windows Services project template is not available in the Express edition of Visual Studio.
At a minimum, to create a functional service you must:
Set the ServiceName property.
Create the necessary installers for your service application.
To create a Windows Service application
Create a Windows Service project.
For instructions on writing a service without using the template, see How to: Write Services Programmatically.
In the Properties window, set the ServiceName property for your service.
Set any of the following properties to determine how your service will function.
Property Setting CanStop
Trueto indicate that the service will accept requests to stop running;
falseto prevent the service from being stopped.
Trueto indicate that the service wants to receive notification when the computer on which it lives shuts down, enabling it to call the OnShutdown procedure.
Trueto indicate that the service will accept requests to pause or to resume running;
falseto prevent the service from being paused and resumed.
Trueto indicate that the service can handle notification of changes to the computer's power status;
falseto prevent the service from being notified of these changes.
Trueto write informational entries to the Application event log when your service performs an action;
falseto disable this functionality. For more information, see How to: Log Information About Services. Note: By default, AutoLog is set to
Override any other methods for which you want to define functionality.
Add the necessary installers for your service application. For more information, see How to: Add Installers to Your Service Application.
Build your project by selecting Build Solution from the Build menu.
Do not press F5 to run your project — you cannot run a service project in this way.
Install the service. For more information, see How to: Install and Uninstall Services.
- Introduction to Windows Service Applications
- How to: Write Services Programmatically
- How to: Add Installers to Your Service Application
- How to: Log Information About Services
- How to: Start Services
- How to: Specify the Security Context for Services
- How to: Install and Uninstall Services
- Walkthrough: Creating a Windows Service Application in the Component Designer