dotnet-background-services

Implement resilient background processing for .NET hosted services with cancellation and backoff.

Updated Aug 16, 2025
One-click install
npx skills add https://github.com/dodyg/blue-nile-pds --skill dotnet-background-services-dodyg
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-background-services
Source: https://github.com/dodyg/blue-nile-pds/tree/main/.agents/skills/dotnet-background-services
Command: npx skills add https://github.com/dodyg/blue-nile-pds --skill dotnet-background-services-dodyg

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides clear patterns and best practices for implementing reliable long-running background work in .NET applications to avoid tight polling loops, improper scoped service usage, and ungraceful shutdowns.

Core Features & Use Cases

  • BackgroundService and IHostedService patterns for continuous workers and startup/shutdown hooks.
  • Lifecycle guidance including startup ordering, ExecuteAsync behavior, and reverse-order shutdown.
  • Graceful shutdown and cancellation handling with stopping tokens, recommended backoff strategies, and host shutdown timeout configuration.
  • Channel-backed work queues for scalable producer/consumer workflows and safe queue draining.
  • Use Case: process an order queue with a channel-backed singleton queue and a BackgroundService consumer that creates scopes per work item and respects cancellation during deployment or host shutdown.

Quick Start

Start a BackgroundService that polls a channel-backed work queue, creates scopes per job, and respects the stopping token for graceful shutdown.

Frequently Asked Questions about dotnet-background-services

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

FAQPage Schema
How do I implement a .NET BackgroundService with graceful shutdown?

To implement a .NET BackgroundService with graceful shutdown, use cancellation tokens to respect the host's stopping token and configure host shutdown timeouts to ensure safe cleanup during deployment or application termination.

What is the best way to handle background queue processing in .NET?

The best way to handle background queue processing in .NET is using channel-backed work queues. This pattern enables scalable producer/consumer workflows with a singleton queue and a BackgroundService consumer that safely drains the queue.

How does IHostedService lifecycle ordering affect startup and shutdown?

IHostedService lifecycle ordering dictates that services start in registration order during startup and shut down in reverse order. This ensures dependencies are initialized correctly and cleaned up safely when the host stops.

Can I use scoped services inside a .NET BackgroundService?

You can use scoped services inside a .NET BackgroundService by creating a scope per work item. This avoids improper scoped service usage and ensures dependencies like Entity Framework contexts are resolved safely within the background worker.

Why does my long-running worker tight-poll and cause high CPU usage?

A long-running worker tight-polls and causes high CPU usage when it lacks proper backoff strategies. Implementing error backoff strategies and using a PeriodicTimer prevents tight polling loops in background processing.

Do I need channels for periodic jobs in .NET hosted services?

You do not need channels for periodic jobs in .NET hosted services if there are no producer/consumer workflows. Channels are specifically for scalable queue polling and work distribution, while periodic jobs only require a timer-based BackgroundService.