Skill page Distributed 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 skills

Integrate ManagedCode.Orleans.Graph into an Orleans-based .NET application for graph-oriented relationships, edge management, and traversal logic on top of Orleans grains.

dotnet skills install managedcode-orleans-graph

Use ManagedCode.Orleans.SignalR when a distributed .NET application needs Orleans-based coordination of SignalR real-time messaging, hub delivery, and grain-driven push flows.

dotnet skills install managedcode-orleans-signalr
v2.1.0

Build or review distributed .NET applications with Orleans grains, silos, persistence, streaming, reminders, placement, transactions, serialization, event sourcing, testing, and…

dotnet skills install orleans