csharp-background-service

Implement .NET BackgroundService and IHostedService lifecycle with CancellationToken propagation and scoped services.

Updated Mar 1, 2026
One-click install
npx skills add https://github.com/CloudyWing/ai-dotfiles --skill csharp-background-service
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-background-service
Source: https://github.com/CloudyWing/ai-dotfiles/tree/main/skills/csharp-background-service
Command: npx skills add https://github.com/CloudyWing/ai-dotfiles --skill csharp-background-service

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Provides clear, practical guidelines to implement and review .NET background workers so they run reliably without crashing the host, leak scoped services, or block shutdown.

Core Features & Use Cases

  • Selection Guidance: When to choose BackgroundService versus IHostedService for long-running loops or one-time initialization.
  • Resilience Patterns: Proper CancellationToken propagation, loop-level exception handling, OperationCanceledException filtering, and use of PeriodicTimer to avoid overlapping ticks.
  • Queue-based Workers: Channel-based producer-consumer queues with scoped service resolution via IServiceScopeFactory or IDbContextFactory for safe background processing.
  • Use Case: Implement an order synchronization worker, periodic metrics collector, or HTTP-request-backed background job queue that survives errors and stops cleanly.

Quick Start

Ask the skill to review a BackgroundService implementation and report missing CancellationToken propagation, direct injection of scoped services, unhandled exceptions in loops, and registration/start-stop ordering issues.

Frequently Asked Questions about csharp-background-service

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent a .NET BackgroundService from crashing the host when an exception occurs?

To prevent a .NET BackgroundService from crashing, implement loop-level exception handling within your execute method. Filter OperationCanceledException for graceful shutdowns and ensure unhandled errors are caught to keep periodic tasks and queue consumers running reliably.

Why does injecting a scoped service into an IHostedService cause issues, and how do I resolve it?

Injecting scoped services directly into an IHostedService causes captive dependency problems. Resolve this by using IServiceScopeFactory or IDbContextFactory to create scopes for safe database access and resource management during background processing.

What is the best way to handle CancellationToken propagation in a hosted service?

CancellationToken propagation in a hosted service requires passing the token to all asynchronous operations. Use PeriodicTimer to avoid overlapping ticks and ensure the token triggers a clean shutdown without leaking resources.

How do I build a queue-based worker using .NET Channels and BackgroundService?

Build a queue-based worker by implementing a Channel-based producer-consumer pattern. Combine this with IServiceScopeFactory in your BackgroundService to safely resolve scoped dependencies and process queued background jobs.

When should I choose BackgroundService over IHostedService for .NET background tasks?

Choose BackgroundService for long-running loops and continuous queue consumers. Use IHostedService when you only need one-time initialization or need precise control over the registration and start-stop ordering.

How do I ensure correct registration and shutdown ordering for multiple hosted services?

Correct registration and shutdown ordering for multiple hosted services is managed by the .NET generic host. Services stop in reverse order of their start sequence, ensuring clean application shutdown when CancellationToken is triggered.