background-jobs

Implement background job scheduling with Hangfire and IHostedService in .NET applications.

4|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill background-jobs-faysilalshareef
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: background-jobs
Source: https://github.com/FaysilAlshareef/dotnet-ai-kit/tree/main/skills/infra/background-jobs
Command: npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill background-jobs-faysilalshareef

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Centralizes and simplifies background processing in .NET projects so scheduled tasks, fire-and-forget work, and long-running workers run reliably, are observable, and survive restarts.

Core Features & Use Cases

  • Hangfire integration for persistent, retryable, and dashboard-visible jobs.
  • Hosted IHostedService and BackgroundService patterns for polling, cleanup, and startup tasks such as database migrations.
  • Recurring job scheduling with cron expressions, fire-and-forget enqueuing, and recommended patterns for scoped IServiceScopeFactory usage and logging.
  • Use cases include nightly report generation, email delivery after HTTP responses, outbox cleanup, and service startup migrations.

Quick Start

Add a Hangfire recurring job for daily reports at 2 AM and register a BackgroundService for periodic outbox cleanup in your ASP.NET Core project.

Frequently Asked Questions about background-jobs

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

FAQPage Schema
How do I schedule recurring background jobs in ASP.NET Core?

To schedule recurring background jobs in ASP.NET Core, use Hangfire with cron expressions for persistent, retryable tasks, or implement IHostedService and BackgroundService patterns for polling loops and startup migrations.

What is the best way to run fire-and-forget tasks after an HTTP response in .NET?

The best way to run fire-and-forget tasks like email delivery after an HTTP response in .NET is enqueuing them via Hangfire, which provides a persistent job store ensuring work survives application restarts.

How do I use scoped services like DbContext inside a BackgroundService?

To use scoped services like DbContext inside a BackgroundService, inject IServiceScopeFactory to create a scope manually, resolving scoped dependencies safely within your hosted polling or cleanup loops.

Does Hangfire require a persistent job store for background processing?

Yes, Hangfire requires a persistent job store such as SQL Server to manage retryable, dashboard-visible background jobs, ensuring scheduled tasks survive application restarts reliably.

When should I use IHostedService instead of Hangfire for background work?

Use IHostedService or BackgroundService instead of Hangfire for in-process startup tasks like database migrations, periodic polling, or cleanup loops that do not require external persistent storage or a dashboard.