Create Windows Services

In .Net Core 3.0

LAI TOCA
3 min readDec 2, 2019
Ref: https://build5nines.com/net-core-3-0-released-net-5-vnext/

As we known .Net Core 3.0 has been official launched in the end September 2019. Build up Windows Service become more easily using .Net Core 3. If you might curios how old version of .Net Core for creating Windows Service: please refer to this article.

Installation and initialization

We could download .Net Core 3.0 SDK Here (pre-work).

Opening Visual Studio, “Create a new project” => “Worker Service”:

Worker Service — I: search key word: worker
Worker Service — II: setup project location
Worker Service — III: created
If you could not find the worker service template, please try to update your visual studio 2017/2019 to the latest version.

The template project will create below classes:

Demo project structure
Brief description what template created these files for us- Program.cs: Entry point class that enable us to configure and trigger the worker
- Worker.cs: The class inherit the BackgroundService class that implement the interface IHostedService. The method "ExecuteAsync" would be invoked while the method "StartAsync" was been triggered.
- appsettings.{env}.json: File(s) for storing configuring sections (Logging/Connection/Path...)for diverse environments.

Implement our worker for Windows Service

Install extension package: Microsoft.Extensions.Hosting.WindowsService, so that we could convert our worker project into “Windows Service”:

Extension package

We have completed our worker as above code snippet. The worker execute one simplest task: write testing message into file per second while worker been triggered. The next step we could find out our application execute path after we build the solution then key down below command for service registering.

sc create "Demo Service" binPath="{your_application_folder_path}\{your_application}.exe"

And we could try to start your service:

Demonstrate service start/stop

Moreover, you could also setup logging provider to “EventLogLogger”, that’s means we could have trace logs over event viewer.

Install extension package: Microsoft.Extensions.Logging, so we could configure our logging provider and setting according section “Logging” under appsetting.{env}.json file(s).

Event viewer

Summary

Highlight what .Net Core 3 (worker service) brought to us:

🖋 Worker service will generate execute file for us automatically so that we could deploy and register our application as windows service to OS.🖋 Worker service provide us intuition way to bind application logs to any kinds of logging provider, for instance we could export our logs to event viewer that might be helpful for tracking issue over windows service.🖋 Worker service not only applying on console application but web application would also works as charm.🖋 Worker service not limit on Windows platform now, we could also running as systemd process on Linux. If you got interested, see below reference.

Reference

--

--

LAI TOCA
LAI TOCA

Written by LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)

Responses (1)