What is Hangfire?
Hangfire is an open-source and well-documented task scheduler for ASP.NET and ASP.NET Core and is completely free for commercial use. It's multi-threaded, easily scalable, and offers a variety of job types. It's well-structured, simple to use, and gives a powerful performance.
Why is Hangfire used?
It is mainly used to perform background tasks such as batch/email notification, batch import of files, video/image processing, database maintaining, file purging, etc. Hangfire can be used for background tasks with high/low CPU consumption, short/long running tasks, recurring jobs, fire and forget, and many more.
Hangfire Dashboard and Database
Hangfire Dashboard, we can see all the running and scheduled jobs we create with our Hangfire client. We can also monitor servers, job retries, and failed jobs and monitor all jobs in the queue. Another great thing we can do in the Dashboard is manually trigger any existing jobs.
For storing job definitions and statuses, Hangfire can use a SQL Server database by default, and we can also choose other options.
Different Types of Jobs in Hangfire
Different types of jobs that are available in Hangfire.
- Fire-and-Forget Jobs
Fire-and-forget jobs are executed only once and almost immediately after creation. - Delayed Jobs
Delayed jobs are executed only once, but not immediately, after a specific time interval. - Recurring Jobs
Recurring jobs fire many times on the specified CRON schedule. - Continuations
Continuations are executed when its parent job has been finished.
There are also two jobs available in Hangfire, but these are present in the Hangfire Pro version and used to execute multiple jobs in batch as a single entity, which are below.
- Batches
A batch is a group of background jobs created atomically and considered a single entity. - Batch Continuations
Batch continuation is fired when all background jobs in a parent batch are finished.
Set up Hangfire in ASP.NET Core
Step 1
Let's create a new ASP.NET Core Web API project in Visual Studio 2022

Step 2
Provide the Project name HangfireDemo and then provide the location

Step 3
Provide some additional information like .NET 6, Configure for HTTPS, and enable Open API support

Step 4
Install the below NuGet packages :
- Hangfire.AspNetCore
- Hangfire.SqlServer

Step 5
Create a new API Controller named as HomeController
using Hangfire;
using Microsoft.AspNetCore.Mvc;
namespace HangfireDemo.Controllers
{
[ApiController]
public class HomeController : Controller
{
[HttpGet]
[Route("FireAndForgetJob")]
public string FireAndForgetJob()
{
//Fire - and - Forget Jobs
//Fire - and - forget jobs are executed only once and almost immediately after creation.
var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Welcome user in Fire and Forget Job Demo!"));
return $"Job ID: {jobId}. Welcome user in Fire and Forget Job Demo!";
}
[HttpGet]
[Route("DelayedJob")]
public string DelayedJob()
{
//Delayed Jobs
//Delayed jobs are executed only once too, but not immediately, after a certain time interval.
var jobId = BackgroundJob.Schedule(() => Console.WriteLine("Welcome user in Delayed Job Demo!"), TimeSpan.FromSeconds(60));
return $"Job ID: {jobId}. Welcome user in Delayed Job Demo!";
}
[HttpGet]
[Route("ContinuousJob")]
public string ContinuousJob()
{
//Fire - and - Forget Jobs
//Fire - and - forget jobs are executed only once and almost immediately after creation.
var parentjobId = BackgroundJob.Enqueue(() => Console.WriteLine("Welcome user in Fire and Forget Job Demo!"));
//Continuations
//Continuations are executed when its parent job has been finished.
BackgroundJob.ContinueJobWith(parentjobId, () => Console.WriteLine("Welcome Sachchi in Continuos Job Demo!"));
return "Welcome user in Continuos Job Demo!";
}
[HttpGet]
[Route("RecurringJob")]
public string RecurringJobs()
{
//Recurring Jobs
//Recurring jobs fire many times on the specified CRON schedule.
RecurringJob.AddOrUpdate(() => Console.WriteLine("Welcome user in Recurring Job Demo!"), Cron.Hourly);
return "Welcome user in Recurring Job Demo!";
}
[HttpGet]
[Route("BatchesJob")]
public string BatchesJob()
{
//Batches - This option is available into hangfire Pro only
//Batch is a group of background jobs that is created atomically and considered as a single entity.
//Commenting the code as it's only available into Pro version
//var batchId = BatchJob.StartNew(x =>
//{
// x.Enqueue(() => Console.WriteLine("Batch Job 1"));
// x.Enqueue(() => Console.WriteLine("Batch Job 2"));
//});
return "Welcome user in Batches Job Demo!";
}
[HttpGet]
[Route("BatchContinuationsJob")]
public string BatchContinuationsJob()
{
//Batch Continuations - This option is available into hangfire Pro only
//Batch continuation is fired when all background jobs in a parent batch finished.
//Commenting the code as it's only available into Pro version
//var batchId = BatchJob.StartNew(x =>
//{
// x.Enqueue(() => Console.WriteLine("Batch Job 1"));
// x.Enqueue(() => Console.WriteLine("Batch Job 2"));
//});
//BatchJob.ContinueBatchWith(batchId, x =>
//{
// x.Enqueue(() => Console.WriteLine("Last Job"));
//});
return "Welcome user in Batch Continuations Job Demo!";
}
}
}






shgsfhg fghdfhgPosted May 8, 2023, 11:44 AM
Thank you, also can you please share some tips abouts implementing upload file in background ?