Featured

Hangfire in .NET 6 – Background Jobs Made Easy

Last modified: March 19, 2022

Why we need Hangfire?

There are couple of possible reason; no access to server (therefore no task schedular), able to manage/view jobs in Web UI.

1. Create Project

Create an ASP.NET MVC application.

createproject

createproject

createproject

createproject

2. Install Hangfire

Right click the project and click Manage NuGet package

Install below packages

  • Hangfire.Core
  • Hangfire.SqlServer
  • Hangfire.AspNetCore

install

install

install

3. Create a database

  • Login to SQL Mangement Studio
  • Create a database

db

4. Configure Settings

  • Add Database Connection string to appsettings.json file

setting

  • Program.cs

setting

setting

5. Jobs

setting

6. View Dashboard

domain.com/hangfire

setting

Secure dashboard

program.cs

app.UseHangfireDashboard("/hangfire", new DashboardOptions { DashboardTitle = "My Website", Authorization = new[] { new HangfireCustomBasicAuthenticationFilter{ User = builder.Configuration.GetSection("HangfireSettings:UserName").Value, Pass = builder.Configuration.GetSection("HangfireSettings:Password").Value } } });

appsettings.json

"HangfireSettings": { "UserName": "admin", "Password": "password" }

setting

setting