# Hangfire — Background Job Processing for .NET > A .NET library for running background jobs reliably with persistence, automatic retries, and a built-in monitoring dashboard. ## Install Save as a script file and run: # Hangfire — Background Job Processing for .NET ## Quick Use ```bash dotnet add package Hangfire dotnet add package Hangfire.SqlServer ``` ```csharp builder.Services.AddHangfire(x => x.UseSqlServerStorage(connStr)); builder.Services.AddHangfireServer(); // Enqueue a fire-and-forget job BackgroundJob.Enqueue(() => Console.WriteLine("Hello from background!")); ``` ## Introduction Hangfire is a .NET library for performing background processing in web and console applications. It persists job state to a database, so jobs survive application restarts. It includes a built-in dashboard for monitoring and managing queues, retries, and scheduled tasks. ## What Hangfire Does - Runs fire-and-forget jobs that execute once in the background - Schedules delayed jobs to run at a future time - Manages recurring jobs using CRON expressions - Provides continuations that chain jobs together - Persists all job state to SQL Server, PostgreSQL, Redis, or other storage ## Architecture Overview Hangfire serializes job method calls and their arguments into a storage backend. A background server polls for pending jobs and executes them in worker threads. Each job transitions through states (Enqueued, Processing, Succeeded, Failed) tracked in the database. The dashboard reads this state data to provide a real-time web UI. Automatic retry with exponential backoff handles transient failures. ## Self-Hosting & Configuration - Install core and storage packages via NuGet - Register services with `AddHangfire()` and `AddHangfireServer()` in Program.cs - Map the dashboard with `app.UseHangfireDashboard()` for monitoring - Configure worker count, queues, and retry policies in server options - Secure the dashboard with authorization filters in production ## Key Features - Persistent job storage that survives application restarts and deployments - Built-in web dashboard with real-time job monitoring - Automatic retries with configurable attempt counts and backoff - CRON-based recurring job scheduling - Batch and continuation job support (Pro edition) ## Comparison with Similar Tools - **Quartz.NET** — job scheduler without persistence dashboard; more scheduling power, less monitoring - **Azure Functions (Timer)** — cloud-native serverless scheduling; no self-hosted option - **Celery (Python)** — distributed task queue for Python; different ecosystem, similar concepts - **Coravel** — lightweight .NET task scheduler; simpler, no persistence or dashboard - **WorkerService** — .NET BackgroundService base class; manual implementation, no persistence ## FAQ **Q: What databases does Hangfire support?** A: The open-source version supports SQL Server. Community packages add PostgreSQL, MySQL, MongoDB, Redis, and SQLite. Some storage providers are maintained by third parties. **Q: Is the dashboard secure by default?** A: In development it allows anonymous access. In production, add an IDashboardAuthorizationFilter to restrict access to authenticated administrators. **Q: What is the difference between Hangfire and Hangfire Pro?** A: Hangfire (open source, LGPL) covers fire-and-forget, delayed, recurring, and continuation jobs. Hangfire Pro adds batch jobs, Redis storage, and enhanced performance features under a commercial license. **Q: Can Hangfire run in a web farm?** A: Yes. Multiple Hangfire servers can process jobs from the same storage. The storage backend handles coordination and ensures each job is processed exactly once. ## Sources - https://github.com/HangfireIO/Hangfire - https://www.hangfire.io/ --- Source: https://tokrepo.com/en/workflows/asset-b8df539c Author: Script Depot