C# tutorials > Frameworks and Libraries > Other Important Libraries > Hangfire for background job processing
Hangfire for background job processing
Hangfire is an open-source framework for .NET that enables you to perform background processing in your applications. It's particularly useful for offloading long-running or resource-intensive tasks from the main application thread, improving responsiveness and overall performance. This tutorial provides a comprehensive guide on how to use Hangfire for background job processing in C#.
Setting up Hangfire in your project
First, you need to install the Hangfire NuGet packages. Hangfire.AspNetCore
provides the necessary components for integrating Hangfire into an ASP.NET Core application, while Hangfire.SqlServer
configures Hangfire to use SQL Server as its storage.
Install-Package Hangfire.AspNetCore
Install-Package Hangfire.SqlServer
Configuring Hangfire in Startup.cs
In your Startup.cs
file, configure Hangfire services and middleware.
AddHangfire
: Configures Hangfire with SQL Server storage. Replace "HangfireConnection"
with the name of your connection string in appsettings.json
. The SqlServerStorageOptions
are highly recommended and improve performance.AddHangfireServer
: Registers the Hangfire server, which processes background jobs.UseHangfireDashboard
: Enables the Hangfire dashboard, which provides a UI for monitoring and managing jobs. This endpoint is typically at /hangfire
.UseHangfireServer
: Starts the Hangfire server.RecurringJob.AddOrUpdate
: Creates a recurring job that runs on a schedule. In this example, a job is scheduled to run every 5 minutes. Note that backgroundJobs.Enqueue does not work anymore.
// In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
services.AddHangfireServer();
// Other service configurations...
}
public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IWebHostEnvironment env)
{
app.UseHangfireDashboard();
app.UseHangfireServer();
// Example: Enqueue a recurring job
// backgroundJobs.Enqueue(() => Console.WriteLine("Hello from Hangfire!")); // no longer working on .net 6, must use RecurringJob.AddOrUpdate instead
RecurringJob.AddOrUpdate("recurring-job", () => Console.WriteLine("Recurring job executing!"), Cron.MinuteInterval(5));
// Other app configurations...
}
Defining and Enqueuing a Background Job
This example demonstrates how to define a background job and enqueue it using IBackgroundJobClient
.
MyService.LongRunningTask
: This is the method that will be executed in the background.IBackgroundJobClient
: This interface is used to enqueue background jobs. Inject it into your controller or service._backgroundJobClient.Enqueue
: This method enqueues the LongRunningTask
method to be executed by the Hangfire server.
public class MyService
{
public void LongRunningTask(string parameter)
{
// Simulate a long-running task
Thread.Sleep(5000);
Console.WriteLine($"Task completed with parameter: {parameter}");
}
}
// In a controller or service
public class MyController : ControllerBase
{
private readonly IBackgroundJobClient _backgroundJobClient;
public MyController(IBackgroundJobClient backgroundJobClient)
{
_backgroundJobClient = backgroundJobClient;
}
[HttpGet("enqueue")]
public IActionResult EnqueueJob(string parameter)
{
_backgroundJobClient.Enqueue(() => new MyService().LongRunningTask(parameter));
return Ok("Job enqueued!");
}
}
concepts behind the snippet
Hangfire employs several key concepts to facilitate background job processing:
Real-Life Use Case Section
Imagine an e-commerce application where processing orders involves multiple steps, including inventory updates, payment processing, email notifications, and generating reports. All these tasks don't need to happen inline, the user can have an 'order confirmation' while these tasks are running. Using Hangfire, you can offload these tasks to the background, allowing the web application to remain responsive and available to handle new requests. This improves the overall user experience and prevents the application from becoming bogged down by long-running operations.
Best Practices
SqlServerStorageOptions
to optimize performance. Ensure your SQL Server has sufficient resources (CPU, Memory, Disk).
Interview Tip
When discussing Hangfire in an interview, be prepared to discuss:
When to use them
Hangfire is ideal for scenarios where you need to perform tasks that:
Memory footprint
Hangfire's memory footprint depends on several factors, including: To minimize Hangfire's memory footprint, you can:
Alternatives
Alternatives to Hangfire for background job processing in .NET include:
Pros
Cons
FAQ
-
How do I handle exceptions in Hangfire jobs?
Use a
try-catch
block within your job and log the exception. You can also configure Hangfire to automatically retry failed jobs. -
How do I monitor Hangfire jobs?
Use the Hangfire dashboard, which is accessible by default at
/hangfire
after configuration. -
How do I secure the Hangfire dashboard?
You can implement authorization filters to restrict access to the Hangfire dashboard based on user roles or permissions. See the Hangfire documentation for detailed instructions.
-
My Hangfire jobs are not running. What should I check?
Verify the following:
- The Hangfire server is running (
app.UseHangfireServer()
is called inStartup.cs
). - The Hangfire connection string is correctly configured in
appsettings.json
. - The SQL Server (or other storage mechanism) is running and accessible.
- There are no exceptions being thrown during job processing. Check your application logs.
- The Hangfire queues are not paused.
- The Hangfire server is running (