Scheduling background jobs using Hangfire in ASP.NET Core

Scheduling and monitoring background task is challenging work. Every big application needs to implement background tasks to accomplish background works like data processing, email reminders, process SMS queue and Email queues etc. Windows service is that the most typical approach to meet the necessity.

Today, we are going to setup Hangfire and write some code to schedule an initial job in the ASP.NET Core project.

Hangfire is an open source library to schedule and execute background jobs in .NET application. you'll be able to create a simple background process inside the same application pool or thread without creating separate application. Hangfire create background jobs in persistence storage like MS SQL server, Redis, MongoDb and others, that may prevent from loosing job on recycling IIS pool or exception prevalence.

ASP.NET Core is now a common platform for MVC and Web API, no separate project creation needed. Let's create a new ASP.NET Core MVC project. After the project is created install Hangfire from nuget. You can install Hangfire either from Package Management Console or Nuget package manager.

Intall via nuget

PM> Install-Package HangFire

We are going to install Hangfire using Nuget package manager. Search the Hangfire stable version(current 1.6.7) and install in the project.

Image 1: Nuget package manager

After successful installation, let's configure Hangfire.

Configuration

Open Startup.cs class and locate a ConfigureServices method to register Hangefire as a service.

Here we are registering Hangfire will SQL Server. We must provide a connection string to locate SQL server database named HangFireDb. DefaultConnection is a connection string name added to appsettings.json file.

Once the service is configured, navigate to Configure method to add below codes. Here, app.UseHangfireDashboard() will set up the dashboard http://<website-url>/hangfire, and app.UseHangfireServer() will register a new instance for BackgroundJobServer.

Now, Let's run the application. Hangfire dashboard is available in browser by hitting http://<website-url>/hangfire url.

Image 2: Hangfire Dashboard.

Hangfire offers integrated web monitoring UI, which is used to control any aspect of background job processing, as well as statistics, exceptions and background job history.
When the Hangfire server starts, it'll look for configured database and check for the required database schema. If schema doesn't exist, it creates schema. Below image shows a list of schemas created when running the application.

Image 3: Hangfire database schema

How it Works?

Hangfire handles different types of background jobs, and all of them are invoked in a separate execution context.
With Hangfire you can create

Fire-and-forget

Fire and forget jobs are executed once on an immediate basis after creation. Once you create a fire-and-forget job, it is saved to its queue ("default" by default, but multiple queues supported). The queue is listened by a couple of dedicated workers that fetch a job and perform it.

After running the application, we have interesting results in a dashboard.

Image 4: A list of succeeded jobs

As you can see, dashboard gives meaningful information like a list of succeeded jobs with Id, name, executed time and duration. Re-queuing jobs features has been provided to re-fire selected job from the list.

Following are the screenshot for various kind of background jobs.

Image 5: Fire and forget job example

Image 6: Delayed job example

Image 7: Continuations job example

Image 8: Continuations dependent job example

Image 9:Recurring job example

Change the Dashboard Url

Be default Hangfire set http://<website-url>/hangfire to access the dashboard. It's easy to configure your custom url.

app.UseHangfireDashboard("/dashboard");

Access Remotely

Default Hangfire configuration set up for local environment only. To make Hangfire working on production environment, we need to implement custom IAuthorizationFilter. So our code changed to this.

Summary

In summary, in this article we've got learned configure the Hangfire in ASP.NET Core. We compose some code to queue the background jobs using Hangfire in ASP.NET Core. I I emphatically favored Hangfire, as it is very easy to implement and dashboard allows you to monitor and control any aspect of background job
processing, as well as statistics, exceptions and background job
history.