04-01-2019, 12:34 AM
.NET Core Workers as Windows Services
Glenn
March 29th, 2019
March 29, 2019 (March 29, 2019) by Glenn Condron [MSFT]–>
In .NET Core 3.0 we are introducing a new type of application template called Worker Service. This template is intended to give you a starting point for writing long running services in .NET Core. In this walkthrough we will create a worker and run it as a Windows Service.
Create a worker
Preview Note: In our preview releases the worker template is in the same menu as the Web templates. This will change in a future release. We intend to place the Worker Server template directly inside the create new project wizard.
Create a Worker in Visual Studio
Create a Worker on the command line
Run
Run as a Windows Service
In order to run as a Windows Service we need our worker to listen for start and stop signals from the .NET type that exposes the Windows Service systems to .NET applications. To do this we want to:
Add the NuGet package
Add the call to the in our Program.cs
This method does a couple of things. First, it checks whether or not the application is actually running as a Windows Service, if it isn’t then it noops which makes this method safe to be called when running locally or when running as a Windows Service. You don’t need to add guard clauses to it and can just run the app normally when not installed as a Windows Service.
Secondly, it configures your host to use a. works with to help control the lifetime of your app when run as a Windows Service. This overrides the default that handles signals like CTL+C.
Install the Worker
Once we have our worker using the we then need to install it:
First, lets publish the application. We will install the Windows Service in-place, meaning the exe will be locked whenever the service is running. The publish step is a nice way to make sure all the files I need to run the service are in one place and ready to be installed.
Then we can use the sc utility in an admin command prompt
For example:
Security note: This command has the service run as local system, which isn’t something you will generally want to do. Instead you should create a service account and run the windows service as that account. We will not talk about that here, but there is some documentation on the ASP.NET docs talking about it here: https://docs.microsoft.com/en-us/aspnet/...etcore-2.2
Logging
The logging system has an Event Log provider that can send log message directly to the Windows Event Log. To log to the event log you can add the package and then modify your :
In upcoming previews we plan to improve the experience of using Workers with Windows Services by:
We hope you try out this new template and want you to let us know how it goes, you can file any bugs or suggestions here: https://github.com/aspnet/AspNetCore/issues/new/choose
Glenn
March 29th, 2019
March 29, 2019 (March 29, 2019) by Glenn Condron [MSFT]–>
In .NET Core 3.0 we are introducing a new type of application template called Worker Service. This template is intended to give you a starting point for writing long running services in .NET Core. In this walkthrough we will create a worker and run it as a Windows Service.
Create a worker
Preview Note: In our preview releases the worker template is in the same menu as the Web templates. This will change in a future release. We intend to place the Worker Server template directly inside the create new project wizard.
Create a Worker in Visual Studio
Create a Worker on the command line
Run
Code:
dotnet new workerRun as a Windows Service
In order to run as a Windows Service we need our worker to listen for start and stop signals from
Code:
ServiceBaseAdd the
Code:
Microsoft.Extensions.Hosting.WindowsServicesAdd the
Code:
UseServiceBaseLifetimeCode:
HostBuilderCode:
public class Program
{ public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceBaseLifetime() .ConfigureServices(services => { services.AddHostedService<Worker>(); });
}This method does a couple of things. First, it checks whether or not the application is actually running as a Windows Service, if it isn’t then it noops which makes this method safe to be called when running locally or when running as a Windows Service. You don’t need to add guard clauses to it and can just run the app normally when not installed as a Windows Service.
Secondly, it configures your host to use a
Code:
ServiceBaseLifetimeCode:
ServiceBaseLifetimeCode:
ServiceBaseCode:
ConsoleLifetimeInstall the Worker
Once we have our worker using the
Code:
ServiceBaseLifetimeFirst, lets publish the application. We will install the Windows Service in-place, meaning the exe will be locked whenever the service is running. The publish step is a nice way to make sure all the files I need to run the service are in one place and ready to be installed.
Code:
dotnet publish -o c:\code\workerpubThen we can use the sc utility in an admin command prompt
Code:
sc create workertest binPath=c:\code\workerpub\WorkerTest.exeFor example:
Security note: This command has the service run as local system, which isn’t something you will generally want to do. Instead you should create a service account and run the windows service as that account. We will not talk about that here, but there is some documentation on the ASP.NET docs talking about it here: https://docs.microsoft.com/en-us/aspnet/...etcore-2.2
Logging
The logging system has an Event Log provider that can send log message directly to the Windows Event Log. To log to the event log you can add the
Code:
Microsoft.Extensions.Logging.EventLogCode:
Program.csCode:
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(loggerFactory => loggerFactory.AddEventLog()) .ConfigureServices(services => { services.AddHostedService<Worker>(); });In upcoming previews we plan to improve the experience of using Workers with Windows Services by:
- Rename UseWindowsServiceBaseLifetime to UseWindowsService
- Add automatic and improved integration with the Event Log when running as a Windows Service.
We hope you try out this new template and want you to let us know how it goes, you can file any bugs or suggestions here: https://github.com/aspnet/AspNetCore/issues/new/choose

