Sending emails, clearing away old logs, processing files, and updating reports are just a few examples of the background tasks that are frequently required in real-world systems without interfering with user requests.
Many developers immediately turn to frameworks such as Azure Functions, Quartz.NET, or Hangfire.
However, what if you're looking for something integrated directly into ASP.NET Core, lightweight, and dependency-free?
The answer is:
Hosted Services — a powerful built-in feature in ASP.NET Core to run background tasks efficiently.
This article will show you how to build background jobs without Hangfire, step-by-step.
A background job is any process that runs behind the scenes — not directly triggered by a user and not part of the main request/response flow.
Sending order confirmation emails after checkout
Generating and emailing PDF reports
Cleaning up temporary files
Syncing data with third-party APIs at intervals
These tasks can take time, and you don't want them to slow down user requests.
That's where Hosted Services come in.
ASP.NET Core includes a background task framework out of the box using IHostedService.
You can think of it as a service that starts with your application and runs continuously in the background.
There are mainly two types:
IHostedService– Run logic at startup and shutdown.BackgroundService– A helper base class for continuous or scheduled jobs.
Below is a high-level diagram of how Hosted Services work in ASP.NET Core.
Let's create a simple background job that runs every 5 minutes and cleans up old logs from the database.
Program.csThat's it!
Now your background service will automatically start when the application starts.
When working inside a background service, never directly inject scoped services (like DbContext).
Instead, use IServiceProvider.CreateScope() — as shown above — to create a new service scope.
This ensures clean resource management and prevents memory leaks.
6. Adding Multiple Background Services
You can register multiple hosted services — each handling a different task:
Each one will run independently in its own background thread.
7. Graceful Shutdown and Cancellation
ASP.NET Core automatically handles graceful shutdowns for hosted services.
When the app stops (or is restarted), it sends a cancellation token (stoppingToken) to stop the job safely.
To handle it cleanly:
8. Advanced Scenarios
Scheduled Jobs
You can make jobs run at specific times using Cronos (a lightweight Cron parser):
Then calculate the next occurrence using:
Queue-Based Background Processing
For more complex use cases (like queued background tasks), use BackgroundTaskQueue with Channel or ConcurrentQueue.
9. Benefits of Using Hosted Services
| Feature | Benefit |
|---|---|
| No extra dependency | Built directly into .NET Core |
| Lightweight | Ideal for microservices and small apps |
| Reliable | Starts/stops with the app lifecycle |
| Flexible | Run periodic, continuous, or one-time tasks |
| Secure | Full DI and configuration support |
10. When Not to Use Hosted Services
For long-running or heavy workloads, use Azure Functions, Worker Services, or Hangfire.
For distributed job coordination, prefer message queues (RabbitMQ, Kafka).
Hosted Services are best for simple, internal background tasks within a single app instance.
Conclusion
You don't always need heavy frameworks like Hangfire for background processing.
ASP.NET Core's Hosted Services
provide a clean, native way to manage background jobs — with full
integration into dependency injection, logging, and configuration.
They're:
Simple to build
Reliable to run
Perfect for small to medium workloads
By mastering Hosted Services, you gain a powerful tool to run scheduled, continuous, or on-demand background tasks all without adding any external library.
Windows Hosting Recommendation


0 comments:
Post a Comment