Chapter 08 - Background Tasks With Worker Service
Chapter 08 - Background Tasks With Worker Service
Objectives
Overview Worker Service .NET
How to implement a Background Task by Worker Service
Overview Windows Service
Demo create Worker Service to consume ASP.NET Core Web API
Demo to publish Worker Service as a Windows Service
04/24/2025 2
Understanding Worker Service .NET
A worker service is a .NET project built using a template which supplies a few
useful features that turn a regular console application into something more
powerful
A worker service runs on top of the concept of a host, which maintains the
lifetime of the application. The host also makes available some familiar features,
such as dependency injection, logging and configuration
Worker services will generally be long-running services, performing some
regularly occurring workload such as:
Processing messages/events from a queue, service bus or event stream
Reacting to file changes in an object/file store
Aggregating data from a data store
Enriching data in data ingestion pipelines
Formatting and cleansing of AI/ML datasets
04/24/2025 3
Understanding Worker Service .NET
It’s also possible to develop a worker service which performs a process from
start to finish and then shuts down. Coupled with a scheduler, this can support
periodical batch workloads
For example, every hour the service is started by the scheduler, calculates some
aggregate totals and then shuts down
Worker services do not have a user interface, nor will they support direct user
interaction. They are particularly applicable when designing a microservices
architecture. Here responsibilities are often split into distinct, separately
deployable and scalable services
04/24/2025 4
Understanding Worker Service .NET
Use a host to maintain the lifetime of the console application until the host is
signalled to shut down. Turning a console application into a long-running service
Include features common to ASP.NET Core such and dependency injection,
logging and configuration
Perform periodic and long-running workloads
Background tasks can be implemented in two ways: Implementing
IHostedService Interface and Inheriting BackgroundService Class
04/24/2025 5
The BackgroundService Class
BackgroundService is an abstract base class for implementing a long-running
IHostedService. Using BackgroundService class we can write less code as
compared to IHostedService
The following table describes some of the key methods:
Method Description
StartAsync(CancellationToken) Triggered when the application host is ready to start the service
StopAsync(CancellationToken) Triggered when the application host is performing a graceful shutdown
Performs application-defined tasks associated with freeing, releasing, or resetting
Dispose()
unmanaged resources
Determines whether the specified object is equal to the current object.
Equals(Object)
(Inherited from Object)
This method is called when the IHostedService starts. The implementation should
ExecuteAsync(CancellationToken) return a task that represents the lifetime of the long running operation(s) being
performed
04/24/2025 6
The Worker Service Template Provide
The worker service template includes useful foundational components, like
dependency injection, by default so that we can focus on building our business
logic on top. It includes a host which manages the lifecycle of the application
The worker service template is reasonably basic and includes just three core
files:
Program.cs: The first of which is a
Program class. This class consists of the
required Main method entry point
for .NET console applications. The .NET
runtime expects to locate this method
within the Program class when it starts
your .NET application
04/24/2025 7
The Worker Service Template Provide
appsettings.json: It is one of the common
sources for application configuration. The host
is configured to load application configuration
from several sources when the application
starts using any registered configuration
providers
Worker.cs: The Worker class is something
new which you will not find in the default
ASP.NET Core project template. This is where
the magic of hosted services, combined with
the host, provide the basis of a worker service
04/24/2025 8
Demo 01: Create a Worker Service
using Visual Studio.NET
1. Open Visual Studio.NET , File | New | Project
1
04/24/2025 10
2. Fill out Project name: MyWPFApp and Location then click Next
04/24/2025 11
3. Choose Target Framework: .NET 8 then click Create
04/24/2025 12
4. Run the project
04/24/2025 13
Demo 02: Worker Service and Web API
Worker Service Demo-02
Create a background task that use HttpClient to call a API service to retrieve
the current exchange rate between various currencies, and print the result
1.Open the Visual Studio.NET then create ASP.NET Web API Project named
ExchangeRateService
04/24/2025 15
Worker Service Demo-02
04/24/2025 16
Worker Service Demo-02
5
04/24/2025 17
2.Write codes for the ExchangeRateService project as follows:
04/24/2025 18
3. Right-click on the project, select Open in Terminal. On Developer PowerShell dialog,
execute the following command to run Web API project
04/24/2025 19
4. Open the web browser and enter the following link to test GetLatestRates method with Swagger :
http://localhost:5000/swagger/index.html
04/24/2025 20
Summary
Concepts were introduced:
Overview Worker Service .NET
21