.NET Worker Service: Create a background service in C# (run as a Windows Service)

Round The Code
Round The Code
44.7 هزار بار بازدید - 3 سال پیش - 🖥️ AD Get Surfshark VPN
🖥️ AD Get Surfshark VPN - 82% off FREE + 2 free months 🖥️
https://get.surfshark.net/SH11c

A .NET Worker Service allows for running a C# background service through the use of a hosted service. This can be done with .NET Core, or the newer versions of .NET.

These services can be installed and run as a Windows Service. They can also run in an ASP.NET Core web application.

We'll have a look at how to create a Worker Service in Visual Studio 2022, and the files it creates through the Visual Studio template.

A Worker Service runs as a console application and creates a Host instance to run it.

The Host instance has a ConfigureServices extension method, and this allows us to run hosted services as background tasks. It also allows us to add services as part of dependency injection.

Afterwards, we'll look at the steps to set up a Worker Service so it can be ran as a Windows Service. This includes downloading the Microsoft.Extensions.Hosting.WindowsServices Nuget package and using the AddWindowsServices extension method in the Host instance.

Once the Worker Service application has been published, we'll set up the Worker Service to run as a Windows Service through the use of the "sc create" command in a command prompt. This will allow us to start and stop a Worker Service through the Services app in Windows.

💻 Download the code example for this demo 💻
https://www.roundthecode.com/dotnet-c...

📖 Learn .NET and C# with our online courses 📖
https://www.roundthecode.com/dotnet-c...

► More information: https://www.roundthecode.com/dotnet-t...

► Chapters
0:00 What is a Worker Service?
0:43 Create a Worker Service in Visual Studio
2:46 Use a Worker Service as a Windows Service
7:13 Use logging in background tasks

#workerservice #windowsservice #dotnet
3 سال پیش در تاریخ 1400/11/27 منتشر شده است.
44,710 بـار بازدید شده
... بیشتر