Hangfire Introduction and Implementation in .NET Core 6 Web API

Jaydeep Patil
5 min readJun 11, 2022

--

Overview

  • Hangfire is open-source and used to schedule the job at a particular event and time.
  • It is also, uses 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 monitor and manage all things easily.

Why Hangfire is required in Background Processing

  • Sometimes we need to do lengthy operations like database updates and database maintenance and managed things on periodically basis.
  • 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 Hangfire. We will look at that one by one

Fire-and-Forget Job:

Fire and Forget jobs are executed only one time after certain conditions which we provide

Delayed Job:

A delayed job is execute only once but after a specific interval of time.

Recurring Job:

Recurring Job is executed many times after specified condition and time interval

Continuations Job:

The continuation job is executed 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

  • Here we created different endpoints based on product scenarios like when a user login to the shopping site it will get a welcome message on an immediate basis using the fire and forget job.
  • After that, we created another endpoint related to the product checklist. That when the user adds a new product to the checklist it will get notified and reminded after a few seconds like you are adding a new product to your checklist.
  • In the third endpoint, we created a job for payment. When the user completes the process then it will get an email immediately using Fire-and-Forget Job and later on when this job is executed the Continuation job will get executed which is the child job and executed after the parent job will get executed.
  • Finally, In the last endpoint, we want to send special offers monthly basis for that we use a recurring job that will execute continuously in the background after specified Cron conditions.

Step 6)

Finally, Let’s configure things in Program.cs Class related to Hangfire like SQL Server Database and middleware which we need.

So here we register a few things related to Hangfire and SQL Server Database.

Step 7)

Now, after you run the Application, this swagger window will open, and using this we will perform operations one by one after hitting API endpoints.

Also, it will create the following tables in the database related to Hangfire to manage jobs.

Step 8)

You can now also open the Hangfire dashboard to manage background running jobs after hitting

same API port in another window https://localhost:7204/hangfire

Now we are going to execute API endpoints one by one and check newly created jobs in the Hangfire dashboard.

First when we hit the login endpoint from swagger UI. Then it will create one job and execute that job immediately as shown in the below images

Here, in Hangfire Dashboard you can see all the details about jobs like succeeded, scheduled, failed, and processing as I have shown in the above images.

Now we hit another endpoint product checkout and it will schedule one background job and execute once after a specified time interval

After this we are going to hit the third endpoint which is to create a continuation job and execute it after its parent job will get executed as shown in the image here first parent job is to execute and after that immediate child job which is the continuation job is executed.

Finally, we are going to hit our last API endpoint and check how recurring job is executed one by one.

Here you can see how recurring job is created and executed one by one after a specific time interval.

So, this is all about Hangfire which we used in .NET Core to schedule background jobs as per our requirements.

I hope you understand a few things related to Hangfire

Happy Coding!

--

--

Jaydeep Patil

Full Stack Developer | .Net Core API | Angular | SQL Server | Docker | Azure | Python