Steven Chen Blog
Scheduler in .Net Core 2.1 - Hangfire tutorial
17.09.20184 Min Read — In Coding

Scheduler in .Net Core 2.1 - Hangfire tutorial


I used to setup my project scheduler by using windows service, mostly sending emails, then I find a better library to do this task -- Hangfire. There are multiple reasons, the first is Hangfire is free!!! and then it provide dashboard to monitor and control your scheduled tasks, the last point is it is easy to setup.


Before we get into this Hangfire tutorial make sure you compare all the libraries otherwise you need to refactor your application which is waste of time. After compare these options and ask a friend, I decided to use Hangfire to handle the scheduling emails.

Scheduler and Job

This list comes from awesome-dotnet-core

Step 1: Installation

I use NUGET package to install this library. I install these three library.

<PackageReference Include="Hangfire.AspNetCore" Version="1.6.20" />
<PackageReference Include="Hangfire.Dashboard.Authorization" Version="2.1.0" />
<PackageReference Include="Hangfire.SqlServer" Version="1.6.20" />

You can search them in NuGet Package Manager. The version number does not matter.

Because I use .Net Core 2.1 so that I install Hangfire.AspNetCore rather than Hangfire please make sure you select the right one for your project. The Hangfire.Dashboard.Authorization is to implement the security feature which means it is optional and the SqlServer is a must-have.

Step 2: Startup Configuration

After the installation let's go to the Startup.cs file then you need to do the following things.

Under the

public void ConfigureServices(IServiceCollection services)

Add the following code. These codes is to configure the Hangfire can access your database.

services.AddHangfire(config =>

Under the Configure

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

Add the following code.


After you run your application you should be able to see your hangfire dashboard now.

by adding hangfire after your root url.

{% asset_img "hangFire.PNG" "Dashboard" %}

Step 3: Security Setup

Before we put this into production we need to make sure that this page is not accessible by others.

This is my new Configure method in Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)


    app.UseHangfireDashboard("/hangfire", new DashboardOptions
        Authorization = new[] { new HangfireAuthorizationFilter() },
    app.UseSignalR(routes =>

    app.UseMvc(routes =>


And My HangfireAuthorizationFilter is like this you can use your own code.

public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter

    public bool Authorize(DashboardContext context)
        var httpContext = context.GetHttpContext();

        return httpContext.User.IsAdmin();


Step 4: Hangfire Controller

The next step is to write the Hangfire jobs this depends on your needs I write the code in my Hangfire controller, but write it in your startup.cs is ok but not easy to maintain.

Here is the example of recurrent jobs.

Step 5: Common mistakes

To be continue

Related Resources:

These are some useful resources, maybe you will find them useful.

Tags:  Programming