Overview
- Hangfire is open-source and used to schedule the job at a particular event and time.
- It is also used to create, process, and manage your background jobs.
- Basically, we use this in Background Processing without user intervention.
- Hangfire is reliable and persistent. It will take care of all things once the job is scheduled.
- Hangfire Dashboard is also available for us to manage all things easily.
Why Hangfire is required in Background Processing
- Sometimes we need to do lengthy operations like database updates and database maintenance
- Batch import from XML, JSON, and YAML files.
- Recurring reports periodically basis.
- Mass Notification on Subscription, Sign up basis.
So, these are things which we are able to do periodically over a certain period of time as per our requirement.
Prerequisites
- Basic understanding of .NET Core 6 API and C#.
- Visual Studio 2022
- SQL Server
Agenda
- Create .NET Core API using version 6
- Configure Hangfire in .NET
- Look into the different types of Jobs which are present in Hangfire.
There are different types of jobs that are present in the Hangfire. We will look at that one by one
- Fire-and-Forget Job:
Fire and Forget jobs are execute only one time after certain conditions that we provide. - Delayed Job:
A delayed job is execute only once but after a specific interval of time. - Recurring Job:
Recurring Job is execute many times after specified condition and time interval - Continuations Job:
Continuation job is execute when its parent job is executed and finished.
There are also two jobs present now in Hangfire one is Batch Job and another is Batch Continuation but these are present in the Hangfire Pro version and which are used to execute multiple jobs in batch as a single entity.
Step 1
Open Visual Studio 2022 and create a new .NET Core Web API Project

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

Step 3
Also, provide additional information like .NET Framework 6, Configure HTTPS and enable Open API support and swagger

Step 4
Install Hangfire NuGet Package.

Step 5
Create API Controller and name it as ProductController
using Hangfire;
using Microsoft.AspNetCore.Mvc;
namespace HangfireDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
[HttpGet]
[Route("login")]
public String Login()
{
//Fire - and - Forget Job - this job is executed only once
var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Welcome to Shopping World!"));
return $"Job ID: {jobId}. Welcome mail sent to the user!";
}
[HttpGet]
[Route("productcheckout")]
public String CheckoutProduct()
{
//Delayed Job - this job executed only once but not immedietly after some time.
var jobId = BackgroundJob.Schedule(() => Console.WriteLine("You checkout new product into your checklist!"),TimeSpan.FromSeconds(20));
return $"Job ID: {jobId}. You added one product into your checklist successfully!";
}
[HttpGet]
[Route("productpayment")]
public String ProductPayment()
{
//Fire and Forget Job - this job is executed only once
var parentjobId = BackgroundJob.Enqueue(() => Console.WriteLine("You have done your payment suceessfully!"));
//Continuations Job - this job executed when its parent job is executed.
BackgroundJob.ContinueJobWith(parentjobId, () => Console.WriteLine("Product receipt sent!"));
return "You have done payment and receipt sent on your mail id!";
}
[HttpGet]
[Route("dailyoffers")]
public String DailyOffers()
{
//Recurring Job - this job is executed many times on the specified cron schedule
RecurringJob.AddOrUpdate(() => Console.WriteLine("Sent similar product offer and suuggestions"), Cron.Daily);
return "offer sent!";
}
}
}










Vijay Pratap SinghPosted Apr 10, 2023, 6:43 AM
Nice Article..