In today’s software development landscape, it’s common to deal with time-consuming tasks that can significantly impact the performance of web applications. To avoid blocking the main execution thread and ensure a smooth user experience, these tasks are often delegated to background processes. Hangfire is a popular library for .NET that addresses this need by providing an easy-to-use framework for creating, processing, and managing background jobs. In this article, we’ll explore Hangfire’s background processing capabilities, its components, and how to implement it in a .NET application.
Hangfire is an open-source framework that simplifies the implementation of background job processing in .NET applications. It allows developers to offload resource-intensive or time-consuming tasks to background processes, thereby improving the overall performance and responsiveness of the application. Hangfire is particularly useful for tasks such as:
Hangfire supports various job types, including fire-and-forget, delayed, and recurring jobs. It also provides a built-in dashboard for monitoring and managing background jobs, complete with detailed statistics, job history, and real-time updates.
Hangfire comprises four main components:
Client: The client component is responsible for creating and enqueuing background jobs. It generates a job identifier for each job and stores the job in a persistent storage.
Server: The server component fetches queued jobs from the storage and processes them in the background. It also handles job retries and state changes.
Storage: Hangfire uses a storage system to persist job data, ensuring that jobs are not lost in case of application restarts or crashes. It supports various storage providers like SQL Server, PostgreSQL, Redis, and more.
Dashboard: The web-based dashboard provides an interface for monitoring and managing background jobs, allowing developers and administrators to track job progress and troubleshoot issues.
To get started with Hangfire, follow these steps:
Install-Package Hangfire
Install-Package Hangfire.SqlServer
Startup.cs
file to configure Hangfire:using Hangfire;
public void ConfigureServices(IServiceCollection services)
{
// Configure Hangfire storage with SQL Server
services.AddHangfire(config =>
config.UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection")));
// Add Hangfire server
services.AddHangfireServer();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add Hangfire dashboard
app.UseHangfireDashboard();
}
appsettings.json
file to include the Hangfire connection string:{
"ConnectionStrings": {
"HangfireConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=HangfireDemo;Integrated Security=True;"
}
}
Hangfire supports various background job types:
To create and enqueue a fire-and-forget job, use the Enqueue
method:
BackgroundJob.Enqueue(() => Console.WriteLine("Hello, Hangfire!"));
For delayed jobs, use the Schedule
method:
BackgroundJob.Schedule(() => Console.WriteLine("Delayed job"), TimeSpan.FromMinutes(5));
For recurring jobs, use the AddOrUpdate
method:
RecurringJob.AddOrUpdate("jobId", () => Console.WriteLine("Recurring job"), Cron.Minutely);
Hangfire recurring jobs allow you to schedule background tasks to run at regular intervals. You can use the Cron class to define the job schedule:
RecurringJob.AddOrUpdate("jobId", () => Console.WriteLine("Daily job"), Cron.Daily);
You can also use custom cron expressions for more complex scheduling:
RecurringJob.AddOrUpdate("jobId", () => Console.WriteLine("Custom recurring job"), "0 12 * * 1-5");
Hangfire automatically retries failed jobs using an exponential back-off strategy. You can customize the retry attemptsand delay by configuring the AutomaticRetryAttribute
:
[AutomaticRetry(Attempts = 5, DelaysInSeconds = new int[] { 1, 5, 10, 15, 30 })]
public void MyJobMethod()
{
// Job logic here
}
To disable automatic retries for a specific job, set the Attempts
property to 0:
[AutomaticRetry(Attempts = 0)]
public void MyJobMethod()
{
// Job logic here
}
You can also handle job errors using the IElectStateFilter
interface:
public class MyJobExceptionFilter : JobFilterAttribute, IElectStateFilter
{
public void OnStateElection(ElectStateContext context)
{
var failedState = context.CandidateState as FailedState;
if (failedState != null)
{
// Log the error or perform custom error handling
Console.WriteLine($"Job {context.BackgroundJob.Id} failed: {failedState.Exception}");
}
}
}
Then, apply the custom filter to your job method:
[MyJobExceptionFilter]
public void MyJobMethod()
{
// Job logic here
}
Hangfire provides a built-in dashboard that allows you to monitor and manage background jobs. By default, the dashboard is accessible at the /hangfire
route.
To secure the dashboard, you can implement a custom authorization filter:
public class MyHangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
// Implement custom authorization logic, e.g., check if the user is an admin
return true;
}
}
Then, update the Startup.cs
file to apply the custom filter:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add Hangfire dashboard with custom authorization filter
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new[] { new MyHangfireAuthorizationFilter() }
});
}
The dashboard provides various features, such as:
Hangfire is a powerful and flexible framework for background job processing in .NET applications. It allows developers to offload time-consuming tasks to background processes, improving application performance and user experience. With support for various job types, automatic retries, and a built-in dashboard, Hangfire is an invaluable tool for managing and monitoring background jobs in your .NET projects.