Introduction
In this article, We will discuss about the Hangfire, an open source framework, which is used to run background tasks in .NET. In general, we may need an application which run’s in the background based on the code nature, that is, it may take long time to execute and users does not want to bother about those task. We will have what are Background Task in the separate section, in this article.
Hangfire
As said before, Hangfire is an Open source framework, which simplifies the Processing and Managing Background Jobs in a reliable way. In an application, there may be have some occasion to run code in Background, that may be for a long run or may be based on the schedule. Hangfire will simplifies those background jobs, by Creating, Processing and Managing. Hangfire can be used in any type of .NET Applications.
Background Jobs
In the previous two sections, you are noticing about Background Jobs. Now you may think, what is a Background Job and when those are required. In .NET, some codes which needs to be run in the background because running it on the main thread is not appropriate, due to its nature or due to a specific requirement.
As for an example, in this article, we will create Greetings Scheduler application, which sends Greetings in Mail to the members. In simple, say for an organisation, they will have HR application and they will feed employee details. So from that data, they can schedule Birthday Wishes, Anniversary wishes Automatically. They do not need to check every month and send wishes in Mail.
Note:
This is Part one article and in this article we will have a detailed view on the Hangfire, how to install in our application and the background jobs that Hangfire can perform. The next article (last one), we will have a detailed view on Creating, Performing and Managing Background Jobs and we will apply the best practices.
Features of Hangfire
We should know why to use Hangfire over the windows job scheduler.
- Hangfire is Simple to use. We do not need the Windows Scheduler for Background jobs.
- Management and Visibility of the jobs. Hangfire’s provides a Dashboard, so that we can monitor, review, re-run and keep a track of all our jobs.
- Reliability of Jobs. Hangfire ensures that our Background Job fires at least once. It does by storing our jobs in the database. If our jobs stops in a half way due to exception, Hangfire will automatically re-run the job and marks the job completed only when it is completed. How Hangfire marks as completed is, it ensures all the lines of code are executed, then only it marks as completed.
- Can be Distributed or simple.
Hangfire’s Background Jobs
This is section, we will have what are the background job types provided by Hangfire. We will implement these types with our application in the next article. Now let’s have the job types,
- Fire and Forget Jobs
- Delayed Jobs
- Recurring Jobs
- Continuation Jobs
- Batch Jobs
- Batch Continuations
Hangfire Architecture
There are three Components in Hangfire in order to create and process the Background Jobs. The three components are,
- Hangfire Client
- Job Storage
- Hangfire Server

Hangfire Client
The key component is Hangfire Client, which are Hangfire Client libraries used within our application, to create and store the definition of Background Jobs.
Job Storage
All the Background jobs definitions are stored in Job Storage. When the background jobs are completed, its status are stored in the Job Storage component.
Hangfire Server
The component which picks up the background job’s definition from Job Storage and runs the Background Jobs is the Hangfire Server. Hangfire server implements number of dedicated Background Threads, which fetch the jobs and process them. It’s also responsible to keep the storage clean, those details which are not required any more.
All these three components run’s together in order to process our Background Jobs.
Installing Hangfire
Let’s have a look at installing Hangfire NuGet Package in our application.
Using Package Manager Console
Run the below comment in the Package Manager Console, all the required libraries will be installed.
Install-Package Hangfire
Once after successful installation, a READ ME file will be opened, and copy the below statement and paste it in your Start.cs class, in the .NET CORE Application.

Copy the code and paste it in your Startup.cs file, in the ConfigurationServices() method.
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
services.AddHangfireServer();
services.AddControllersWithViews();
}
The detailed code walk through will be done in the next article.
If we run our application, you could able to see Hangfire’s Dashboard by calling the URL, /hangfire.

In the architecture section, we had a look at the Job Storage Component, when we run the application for the first time, Hangfire checks the associated Tables in the Database mentioned in the Application’s configuration. If there is no such SCHEMA and Tables, it will create the associated tables. The tables are listed below,

Conclusion
In this part I article of Hangfire, we have explored features, architecture and installing Hangfire in our application. I hope you all found pretty useful. In my next article (Final one of Hangfire), we will have a detailed view at the Background Jobs in Hangfire and how to implement in our application and other details. Please kindly share your feedback in the comment section and feel free to share in your medias.
Informative one
LikeLiked by 1 person
Thank you
LikeLike