Framework Background Workers v1.0.0

.NET Worker Services

Build long-running .NET background services with `BackgroundService`, Generic Host, graceful shutdown, configuration, logging, and deployment patterns suited to workers and daemons.

Trigger On

  • building long-running background services or scheduled workers
  • adding hosted services to an app or extracting them into a worker process
  • reviewing graceful shutdown, cancellation, queue processing, or health behavior

Workflow

  1. Use BackgroundService as your base class:

- Provides standard StartAsync/StopAsync handling - Focus on implementing ExecuteAsync only - Proper cancellation token management built-in

  1. Handle scoped dependencies correctly:

- Create service scopes for scoped services - No scope is created by default in hosted services

  1. Implement graceful shutdown:

- Propagate cancellation tokens throughout - Complete work promptly when token fires - Avoid ungraceful shutdown at timeout

  1. Keep execution loop thin:

- Move business logic to testable services - Handle exceptions to prevent service crashes - Use PeriodicTimer for scheduled work

  1. Add observability:

- Use health checks for readiness/liveness - Expose metrics and structured logging - Consider distributed locks for multi-instance

Deliver

  • well-behaved worker processes and hosted services
  • predictable startup and shutdown behavior
  • proper scoped dependency handling
  • health checks for production observability
  • retry and poison-message handling for queue work

Validate

  • cancellation token propagated and shutdown honored
  • scoped services resolved within proper scopes
  • exception handling prevents service crashes
  • health checks report accurate worker status
  • runtime behavior visible through logs or telemetry
  • no blocking calls in async context

Related agents

Orchestration agent
8 linked skills

.NET Aspire Orchestrator

Specialist orchestration agent for .NET Aspire work.

agents install aspire-orchestrator

Also works: dotnet agents install aspire-orchestrator

Orchestration agent
5 linked skills

Orleans Specialist

Orleans specialist agent for grain design, silo topology, persistence, streams, transactions, serialization, event sourcing, placement, testing…

agents install orleans-specialist

Also works: dotnet agents install orleans-specialist