wshaddix/dotnet-skills/background-services
Claude Code and agent skill by wshaddix/dotnet-skills
- community
Checking that the SKILL.md file is still in the repository.
Hosted services, background jobs, outbox patterns, and graceful shutdown handling for ASP.NET Core applications. Includes patterns for reliable job processing, distributed systems, and lifecycle management. Use when implementing background processing in ASP.NET Core applications, handling outbox patterns for reliable message delivery, or managing graceful service shutdown.
Install
npx vskill@latest install wshaddix/dotnet-skills/background-servicesSKILL.md
Background Services in ASP.NET Core
Rationale
Background services are essential for offloading work from the request pipeline, processing queues, and handling scheduled tasks. Poorly implemented background services can lead to data loss, orphaned jobs, and resource leaks. These patterns ensure reliable, observable, and gracefully degrading background processing in production applications.
BackgroundService vs IHostedService
| Feature | BackgroundService | IHostedService | |
|---|---|---|---|
| Purpose | Long-running loop or continuous work | Startup/shutdown hooks | |
| Methods | Override ExecuteAsync | Implement StartAsync + StopAsync | |
| Lifetime | Runs until cancellation or host shutdown | StartAsync runs at startup, StopAsync at shutdown | |
| Use when | Polling queues, processing streams, periodic jobs | Database migrations, cache warming, resource cleanup |
Pattern 1: Basic BackgroundService Structure
Use BackgroundService base class for consistent lifecycle management and cancellation support.
Critical Rules for BackgroundService
- Always create scopes --
BackgroundServiceis registered as a singleton. InjectIServiceScopeFactory, not scoped services directly. - Always handle exceptions -- by default, unhandled exceptions in
ExecuteAsyncstop the host. Wrap the loop body in try/catch. - Always respect the stopping token -- check
stoppingToken.IsCancellationRequestedand pass the token to all async calls. - Back off on empty/error -- avoid tight polling loops that waste CPU. Use
Task.Delaywith the stopping token.
Pattern 2: IHostedService for Startup/Shutdown Hooks
Startup Hook (Cache Warming, Migrations)
Startup + Shutdown (Resource Lifecycle)
Pattern 3: Hosted Service Lifecycle
Startup Sequence
IHostedService.StartAsyncis called for each registered service in registration orderBackgroundService.ExecuteAsyncis called afterStartAsynccompletes (it runs concurrently -- the host does not wait for it to finish)- The host is ready to serve requests after all
StartAsynccalls complete
Important: ExecuteAsync must not block before yielding to the caller. The first await in ExecuteAsync is where control returns to the host.
Shutdown Sequence
IHostApplicationLifetime.ApplicationStoppingis triggered- The host calls
StopAsyncon each hosted service in reverse registration order - For
BackgroundService, the stopping token is cancelled, thenStopAsyncwaits forExecuteAsyncto complete IHostApplicationLifetime.ApplicationStoppedis triggered
Pattern 4: Outbox Pattern for Reliable Messaging
Ensure messages are never lost by storing them in the database transactionally before async processing.
Pattern 5: Graceful Shutdown Handling
Implement IHostedLifecycleService for fine-grained control over startup and shutdown sequences.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/background-services/SKILL.md
Popularity
- Repository stars
- 86
- Repository forks
- 14
- 7-day trend score
- 3
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 18 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/wshaddix/dotnet-skills/background-services)