Introduction
Not every task in an application needs to be completed while a user is waiting for a response. Some operations, such as sending emails, generating reports, processing files, or cleaning up old data, can run in the background without affecting the user experience.
This is where .NET Worker Services are useful. They allow developers to build long-running background processes that operate independently of web requests. Worker Services are lightweight, scalable, and well-suited for cloud, on-premises, and containerized environments.
In this article, you'll learn what .NET Worker Services are, how they work, and how to build a simple background processing service.
What Are .NET Worker Services?
A .NET Worker Service is a background application designed to run continuously or execute scheduled tasks without requiring user interaction.
Unlike an ASP.NET Core Web API, which responds to HTTP requests, a Worker Service performs tasks in the background.
Common examples include:
Sending emails
Processing messages from a queue
Generating reports
Synchronizing data
Monitoring system health
Cleaning temporary files
Running scheduled jobs
Worker Services use the .NET Generic Host, which provides built-in support for dependency injection, logging, and configuration.
Why Use Worker Services?
Worker Services offer several advantages for background processing:
Keep long-running tasks separate from web applications
Improve application responsiveness
Support dependency injection
Integrate easily with cloud services
Work well in Docker containers
Can run as Windows Services or Linux daemons
Separating background tasks from your main application also makes the overall system easier to maintain and scale.
Creating a Worker Service
You can create a new Worker Service using the .NET CLI.
dotnet new worker -n BackgroundWorkerDemo
This command creates a project with the basic files needed for a background service.
The generated project already includes a sample worker class that runs continuously until the application stops.
Understanding the Worker Class
The main background logic is placed inside a class that inherits from BackgroundService.
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Background task is running.");
await Task.Delay(5000, stoppingToken);
}
}
}
In this example:
The worker runs continuously.
A log message is written every five seconds.
The service stops gracefully when the application shuts down.
Practical Example
Imagine you're building an e-commerce application.

Jasen FiciPosted Jul 29, 2026, 12:24 PM
We added this to DotNetNews here: https://dotnetnews.co/archive/the-net-news-daily-issue-507/