Hangfire

Hangfire is an open-source library for ASP.NET and ASP.NET Core that allows you to run background tasks in your application. It supports a variety of job types, including:

  • Fire-and-Forget Jobs: Execute a job only once immediately after it is created.

  • Delayed Jobs: Execute a job after a specified delay.

  • Recurring Jobs: Execute a job at specified intervals, such as daily or weekly.

  • Continuations: Execute a job only after a parent job has completed.


  • Simplicity: Hangfire is easy to set up and integrate with ASP.NET and ASP.NET Core applications.

  • Reliability: It guarantees that jobs will be executed even if the application crashes or is restarted.

  • Scalability: Supports horizontal scaling, allowing you to distribute jobs across multiple servers.

  • Dashboard: Provides a built-in dashboard for monitoring and managing jobs.

  • Flexible Storage: Supports various storage options like SQL Server, Redis, MongoDB, and LiteDB.


  1. Install Hangfire: Add the necessary NuGet packages to your project:

    Install-Package Hangfire.AspNetCore
    Install-Package Hangfire.LiteDB
  2. Configure Hangfire: In your Program.cs file, configure Hangfire to use LiteDB for storage:

    using Hangfire;
    using Hangfire.LiteDB;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddHangfire(configuration => {
        configuration.UseLiteDbStorage("litedb.db");
    });
    builder.Services.AddHangfireServer();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    app.UseHangfireDashboard("/cron");
    app.MapControllers();
    
    app.Run();
  3. Creating Recurring Jobs: Add a recurring job that runs at specified intervals using a cron expression:

    RecurringJob.AddOrUpdate("my-recurring-job", () => Console.WriteLine("Recurring job!"), Cron.Daily);
  4. Updating Job Schedules: Update the schedule of an existing recurring job:

    RecurringJob.AddOrUpdate("my-recurring-job", () => Console.WriteLine("Updated recurring job!"), Cron.Hourly);
  5. Removing Jobs: Remove a specific recurring job:

    RecurringJob.RemoveIfExists("my-recurring-job");

    Remove all recurring jobs:

    public void RemoveAllRecurringJobs()
    {
        var recurringJobs = JobStorage.Current.GetConnection().GetRecurringJobs();
        foreach (var job in recurringJobs)
        {
            RecurringJob.RemoveIfExists(job.Id);
        }
    }
  6. Restarting Hangfire Service: Restart the Hangfire service programmatically by stopping and starting the background job server:

    public class HangfireServiceManager
    {
        private readonly IBackgroundProcessingServer _backgroundProcessingServer;
    
        public HangfireServiceManager(IBackgroundProcessingServer backgroundProcessingServer)
        {
            _backgroundProcessingServer = backgroundProcessingServer;
        }
    
        public void RestartService()
        {
            _backgroundProcessingServer.Dispose(); // Stop the server
            // Optionally, wait a bit before starting it again
            GlobalConfiguration.Configuration.UseLiteDbStorage("litedb.db");
            JobStorage.Current = new LiteDbStorage("litedb.db");
            var backgroundJobServer = new BackgroundJobServer(); // Start the server again
        }
    }

Example Controller

Here’s an example of a controller that manages recurring jobs and Hangfire service operations:

Last updated