Building a Windows Service Using .NET Core

Emre Erkoca
Nerd For Tech
Published in
2 min readJun 3, 2021

If you want to work on something regularly, Windows services might be a good solution. I have to repeat some phrases and I wanted to send them to my phone. I thought I can’t create a mobile application for this. Also, I don’t like mobile programming. I’m using the Slack application and I thought I can send them via Slack. I found an article about Slack integration and I created my application. Publishing was a problem for me. I have no server I just have my personal computer. At that time, I thought I can use my application as a Windows Service. I wanted to explain basically how to create a Windows Service.

Firstly, create a new Worker Service .NET Core project.

Install packages below to your project.

  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Hosting.WindowsServices
  • Serilog.AspNetCore

Change Program.cs CreateHostBuilder() method.

We called UseSerilog() method for writing logs to file. We called UseWindowsService() method because we want to use worker service as Windows Service.

Open Worker.cs file.

You can do anything here. Send a message, send an email, backup database, etc.

Create a txt file for logging. Open appsettings.json and add these configuration settings.

Don’t forget to change the path correctly. Otherwise, you can’t see logs in your file.

Publish Worker Service Project

Open the console and go to your project’s directory.

Run this command:

You’ll see bin\release\netcoreapp3.1\publish directory in your project. When you open publish folder, you can see your project’s exe file.

We’ll create a new Windows Service now. Open your console as an administrator and run create service command by your published project’s directory.

You’ll see your Windows Service when you open services.

When you restart your computer, it’ll start automatically. Because we used start= auto when creating a Windows service. If you want you can start it manually for the first time. You’ll see logs in your log file.

If you don’t use your service you can delete it via this command:

sc delete “your service name”

Also, you can review this repository.

--

--