Background processing is an essential part of modern web applications. Tasks such as sending emails, generating reports, processing files, syncing data with external services, or running scheduled maintenance should not block user requests. Hangfire is one of the most popular libraries in the .NET ecosystem for handling these background tasks reliably.

This article covers Hangfire configuration, different types of jobs, and the modern features available in recent versions of Hangfire.

What is Hangfire?

Hangfire is an open-source framework for performing background job processing in .NET and ASP.NET Core applications. It stores job information in persistent storage (such as SQL Server, PostgreSQL, or Redis), ensuring that jobs survive application restarts and server failures.

how hagfire works

Unlike simple background services, Hangfire provides:

  • Persistent job storage

  • Automatic retries

  • Job scheduling

  • Dashboard for monitoring

  • Distributed processing

  • Queue management

  • Cron scheduling

  • Dependency Injection support

When Should You Use Hangfire?

Hangfire is ideal for operations that do not need to complete during the HTTP request.

02_what_is_hangfire

Examples include:

  • Sending emails

  • Processing uploaded files

  • Generating PDF reports

  • Image resizing

  • Import/Export operations

  • Database cleanup

  • Scheduled synchronization

  • Notification processing

  • Cache refresh

  • Data migration

Instead of making users wait, these tasks can run in the background.

Installing Hangfire

Install the required NuGet packages:

Bash
  
dotnet add package Hangfire.AspNetCore
dotnet add package Hangfire.SqlServer

For PostgreSQL:

Bash
  
    dotnet add package Hangfire.PostgreSql

Configuring Hangfire in .NET Core

appsettings.json

JSON
  
 {
  "ConnectionStrings": {
    "HangfireConnection": "Server=.;Database=HangfireDb;Trusted_Connection=True;"
 }

Program.cs (.NET 8)

C#
  
using Hangfire;
using Hangfire.SqlServer;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHangfire(config =>
{
    config.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
          .UseSimpleAssemblyNameTypeSerializer()
          .UseRecommendedSerializerSettings()
          .UseSqlServerStorage(
              builder.Configuration.GetConnectionString("HangfireConnection"));
});

builder.Services.AddHangfireServer();

var app = builder.Build();

app.UseHangfireDashboard();

app.Run();

Hangfire Dashboard

The dashboard provides a web interface for monitoring jobs.

Code
https://localhost:5001/hangfire
06_hangfire_dashboard

The dashboard displays:

  • Processing jobs

  • Scheduled jobs

  • Recurring jobs

  • Failed jobs

  • Deleted jobs

  • Servers

  • Queues

  • Job history

  • Retry information

It also allows manual retries and job deletion.

Creating Your First Background Job

Create a service:

C#
  
    public class EmailService
{
    public void SendWelcomeEmail(string email)
    {
        Console.WriteLine($"Sending email to {email}");
    }
}

Enqueue a job:

C#
BackgroundJob.Enqueue<EmailService>(
x => x.SendWelcomeEmail("[email protected]"));

The job executes immediately after the request finishes.

Types of Hangfire Jobs

Hangfire supports several job types.

types

1. Fire-and-Forget Jobs

Executed only once immediately.

C#
  
    BackgroundJob.Enqueue(() =>
    Console.WriteLine("Job Executed"));

Use cases:

  • Send email

  • Process image

  • Create invoice

  • Generate PDF

2. Delayed Jobs

Execute after a specified delay.

C#
  
    BackgroundJob.Schedule(
    () => Console.WriteLine("Executed after 30 minutes"),
    TimeSpan.FromMinutes(30));

Use cases:

  • Reminder emails

  • Delayed notifications

  • Payment reminders

3. Recurring Jobs

Run on a schedule using Cron expressions.

C#
  
    RecurringJob.AddOrUpdate(
    "DailyReport",
    () => GenerateDailyReport(),
    Cron.Daily);

Other schedules:

C#
  
Cron.Hourly
Cron.Daily
Cron.Weekly
Cron.Monthly
Cron.Yearly

Custom Cron:

C#
"0 */2 * * *"

Runs every two hours.

4. ContinueWith Jobs

Run after another job completes successfully.

C#
var jobId = BackgroundJob.Enqueue(() => StepOne());
BackgroundJob.ContinueJobWith(
    jobId,
    () => StepTwo());

Useful for:

  • Multi-stage workflows

  • ETL processing

  • Report generation pipeline

5. Batches (Hangfire Pro)

Allows multiple jobs to execute together.

C#
  
    BatchJob.StartNew(batch =>
{
    batch.Enqueue(() => Job1());
    batch.Enqueue(() => Job2());
    batch.Enqueue(() => Job3());
});

Useful for:

  • Bulk email sending

  • Import processing

  • Parallel execution

6. Batch Continuations (Hangfire Pro)

Execute another batch after the first completes.

Code
  
Batch A
   ↓
Batch B

Useful for:

  • Data import

  • Data validation

  • Report generation

Dependency Injection Support

Hangfire integrates seamlessly with ASP.NET Core DI.

C#
  
public class NotificationService
{
    private readonly IEmailService _email;

    public NotificationService(IEmailService email)
    {
        _email = email;
    }

    public async Task SendAsync()
    {
        await _email.SendAsync();
    }
}

Schedule the service:

C#
  
    BackgroundJob.Enqueue<NotificationService>(
    x => x.SendAsync());

Job Queues

Separate different workloads.

C#
  
    BackgroundJob.Enqueue(
    () => ProcessEmail());

Specify queue:

C#
  
[Queue("emails")]
public void ProcessEmail()
{
}

Configure server:

C#
  
builder.Services.AddHangfireServer(options =>
{
    options.Queues = new[] { "critical", "emails", "default" };
});

This prevents long-running jobs from blocking high-priority tasks.

Automatic Retries

Hangfire retries failed jobs automatically.

C#
  
[AutomaticRetry(Attempts = 5)]
public void ProcessOrder()
{
}

Disable retries:

C#
  
    [AutomaticRetry(Attempts = 0)]

Job Filters

Filters work similarly to ASP.NET middleware.

Examples:

  • Logging

  • Authorization

  • Retry policy

  • Exception handling

  • Performance tracking

Example:

C#
  
public class LogFilter : JobFilterAttribute
{
}

Monitoring Failed Jobs

The dashboard shows:

  • Exception

  • Stack trace

  • Retry count

  • Processing server

  • Execution duration

MonitorFailedJobs

Developers can:

  • Retry

  • Delete

  • Requeue

without writing custom tooling.

Modern Features in Hangfire

Recent versions of Hangfire provide several improvements that make it suitable for cloud-native and enterprise applications.

modern features

1. Async/Await Support

Hangfire fully supports asynchronous methods.

C#
  
public async Task SendEmailAsync()
{
    await _emailService.SendAsync();
}

BackgroundJob.Enqueue<MyService>(
    x => x.SendEmailAsync());

2. Built-in Dependency Injection

Works naturally with ASP.NET Core's built-in DI container.

No custom job activator is required for most applications.

3. Distributed Job Processing

Multiple application instances can process jobs simultaneously using the same storage.

Code
  
    Server A
      \
       SQL Server
      /
Server B

Only one server executes a specific job.

Perfect for Kubernetes and cloud deployments.

4. Queue Prioritization

Configure multiple queues:

Code
  
critical
emails
reports
default

Critical jobs can execute before less important ones.

5. Dashboard Authorization

Restrict dashboard access.

C#
  
    app.UseHangfireDashboard("/hangfire",
    new DashboardOptions
    {
        Authorization =
        [
            new MyAuthorizationFilter()
        ]
    });

Never expose the dashboard publicly without authentication.

6. Cancellation Token Support

Gracefully stop jobs during application shutdown.

C#
  
 public async Task ProcessAsync(
    CancellationToken token)
{
    while (!token.IsCancellationRequested)
    {
        await Task.Delay(1000, token);
    }
}

7. Multiple Storage Providers

Supported storage includes:

  • SQL Server

  • PostgreSQL

  • Redis

  • MySQL

  • SQLite

  • Oracle

  • In-memory (development/testing)

8. Concurrency Control

Prevent duplicate execution.

C#
  
[DisableConcurrentExecution(60)]
public void GenerateReport()
{
}

Only one instance runs at a time.

9. Rate Limiting (Hangfire Pro/Ace)

Limit execution frequency.

Example:

  • Maximum 10 API calls per minute

  • Maximum 5 report generations simultaneously

Useful for third-party API integrations.

10. Observability and Logging

Hangfire integrates well with modern logging and monitoring tools:

  • Serilog

  • Microsoft.Extensions.Logging

  • Application Insights

  • OpenTelemetry

  • Sentry

This enables centralized monitoring of background job execution.

Best Practices

best practicies
  • Keep jobs small and focused.

  • Prefer asynchronous methods for I/O-bound work.

  • Use Dependency Injection instead of static methods.

  • Separate workloads into dedicated queues.

  • Protect the Hangfire Dashboard with authentication.

  • Configure retry policies appropriately.

  • Use recurring jobs for scheduled tasks rather than timers.

  • Make jobs idempotent so retries don't produce duplicate side effects.

  • Use CancellationToken to support graceful shutdowns.

  • Monitor failed jobs and set up alerts for recurring failures.

Common Use Cases

ScenarioRecommended Job Type
Send welcome emailFire-and-Forget
Generate monthly reportsRecurring
Process uploaded fileFire-and-Forget
Send reminder after 24 hoursDelayed
Import CSV then generate reportContinueWith
Bulk notificationsBatch (Pro)
Database cleanupRecurring
Sync with external APIsRecurring or Queue-based

Conclusion

Hangfire is a robust and production-ready solution for background processing in .NET Core applications. Its persistent storage, automatic retries, scheduling capabilities, and monitoring dashboard make it significantly more reliable than implementing custom background workers for many scenarios.

Whether you need to send emails, generate reports, process files, or schedule recurring maintenance tasks, Hangfire provides a clean programming model with strong integration into ASP.NET Core's dependency injection and logging infrastructure. By organizing jobs into queues, securing the dashboard, and following best practices such as idempotent job design and proper retry handling, teams can build scalable and resilient background processing systems with minimal effort.