Automatically build and deploy .NET core 3 console application as a Windows service
In this post, I want to share an example of using Azure Devops to automatically build and deploy a .NET console application as a windows service to run on a Windows VM.
Enable .NET core 3 app to run as a windows service
For this example, I have scaffolded a simple .NET core console application using Visual Studio. It is a very rudimentary app which logs out a message every minute until stop. You can generate a similar project using Worker Service Template as a starting point. You can checkout the codes for this project on my Github repo.
At the high level, to enable a .NET core app to run as a windows service, you need the following:
- You need to add the nuget package: Microsoft.Extensions.Hosting.WindowsServices
- Call
UseWindowsService()
in Program.cs file, when creating the host builder. - Your service class need to implement
BackgroundService
class. - Register the service by calling the
AddHostedService
extension method.
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services)…