Create a windows service with .NET

Command line

Please refer to my previous article of how to create a service with the topshelf library: https://alexrait.blogspot.com/2023/03/running-topshelf-with-quartz-in-net.html.

This time here is the most simple way of creating a windows service with .NET 8 with no dependency on topshelf. First, install these packages with nuget:

  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Hosting.Abstractions

Create a background service:



public class MyService(ILogger<MyService> logger) : BackgroundService
{
    private readonly ILogger<MyService> _logger = logger;

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("MyService running at: {time}", DateTimeOffset.Now);
            await Task.Delay(1000, stoppingToken);
        }
    }

}

Add this to the main class:


var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<MyService>();

var host = builder.Build();
host.Run();

Use powershell to install the service:


New-Service -Name "MyService" -BinaryPathName "C:\path\to\MyService.exe" -StartupType Automatic 

Another thing to remember is to set the current working directory, or else, the appsettings.json and other files won't be accessible:

Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

Post a Comment

Previous Post Next Post