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:
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.
Warning
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:
In the Properties window, set the ServiceName property for your service.
Note
The value of the ServiceName property must always match the name recorded in the installer classes. If you change this property, you must update the ServiceName property of installer classes as well.
Set any of the following properties to determine how your service will function.
True to indicate that the service wants to receive notification when the computer on which it lives shuts down, enabling it to call the OnShutdown procedure.
True to indicate that the service can handle notification of changes to the computer's power status; false to prevent the service from being notified of these changes.
True to write informational entries to the Application event log when your service performs an action; false to disable this functionality. For more information, see How to: Log Information About Services. Note: By default, AutoLog is set to true.
Note
When CanStop or CanPauseAndContinue are set to false, the Service Control Manager will disable the corresponding menu options to stop, pause, or continue the service.
Access the Code Editor and fill in the processing you want for the OnStart and OnStop procedures.
Override any other methods for which you want to define functionality.
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET
feedback
.NET
is an open source project. Select a link to provide feedback:
See how to add installers to your service application. Visual Studio ships installation components that can install resources associated with your service apps.
Understand service application programming architecture. Windows Service applications are based on a class that inherits from System.ServiceProcess.ServiceBase.
Specify the security context for services. Services run in the default system account context have other system resource access rights than the logged-in user.
In this tutorial, create a Windows service app in Visual Studio that writes messages to an event log. Add features, set status, add installers, and more.
Read an introduction to Windows service applications. Services let you create long-running executable applications that run in their own Windows sessions.