Tutorials ADO.NET Core Tutorial
Background Services — Complete Guide
Background Services — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ADO.NET Core Tutorial on Toolliyo Academy.
On this page
ADO.NET Core Tutorial · Lesson 68 of 100
Background Services
Foundations ✓ → SQL & safety ✓ → Production → Projects
Production · 3 — ASP.NET & enterprise · ~10 min · Module 7: Advanced Enterprise Topics
What is this?
BackgroundService workers run ADO.NET loops for relays, cleanups, and batch jobs inside the host.
Why should you care?
ShopNest outbox relay can live as a hosted service.
See it live — copy this example
Use a .NET console or API project with SQL Server LocalDB. Run dotnet run after pasting.
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await RelayOnceAsync(stoppingToken);
await Task.Delay(1000, stoppingToken);
}
}
What happened?
- Honor cancellation.
- Create scopes for scoped repos.
- Leader election if only one worker should run.
Practice next
- Add hosted relay.
- Scope IServiceScope per tick.
- Delay between polls.
- Batch size 50.
- Stop on shutdown flush.
Remember
Hosted loops. New scope per unit. Cancel cleanly.
ShopNest outbox worker
BackgroundService relays events.
Outcome: API process publishes reliably.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!