Over a million developers have joined DZone.

How to Integrate HangFire With ASP.NET Core 1.1

DZone's Guide to

How to Integrate HangFire With ASP.NET Core 1.1

Read on to learn how to use the open-source, background processing tool, HangFire, along with your ASP.NET Core web application.

· Web Dev Zone ·
Free Resource

Access over 20 APIs and mobile SDKs, up to 250k transactions free with no credit card required

Hangfire is one of the easiest ways to perform background processing in .NET and .NET Core Applications. In this application we are going to learn how we are can integrate Hangfire with ASP.NET Core applications.

About Hangfire:

Hangfire allows you to create background tasks in .NET applications. It’s extremely easy to integrate. It allows you to kick off method calls outside of the request processing pipeline in very easy but reliable ways. You can store those jobs in on-premise SQL Servers, SQL Azure, Redis, or MSMQ.

You can find more information about Hangfire here.

Hangfire also maintains its documentation at this link.

Integrating HangFire With ASP.NET Core 1.1:

To demonstrate how we can integrate with ASP.NET Core, I’m going to create a new ASP.NET Core application in Visual Studio 2017, like the one below.


Once we've clicked on it, it will ask for the Application task we are going to select. Choose Web Applications, as shown below.


As our application is not ready, we are going to install the Hangfire nuget package in the application.


Now that we have added Hangfire to our ASP.NET Core application, we need to create a SQL Server database for our Hangfire application. Here I’m going to use a SQL Server for Job Storage, but there are various Job Storage options available as mentioned above.


Now we need to add the ConnectionString function to connect our application to our database on the backend. Let’s put it on appsetting.json like below.

  "ConnectionStrings": {
    "HangFireConnectionString": "Data Source=SQLServer;Initial Catalog=HangFireSample;User ID=YourUserName;Password=YourPassword;MultipleActiveResultSets=true"
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"

Now, in the startup.cs file, to configure the services method we need to integrate Hangfire, like I've shown below. Here I’ve added Hangfire to our application and also indicated that we are going to use a SQL Server for Job Storage and provided connection strings for the same.

public void ConfigureServices(IServiceCollection services)
    // Add framework services.

Now Hangfire integration is easy to use. When you run it, it will automatically create tables related to Hangfire configurations and storage as shown below.


Now you can easily add background tasks in any place.

BackgroundJob.Enqueue(() => Console.WriteLine("BackGroundJob"));

You can even add recurring jobs which will run at a specific time.

RecurringJob.AddOrUpdate(() => Console.WriteLine("RecurringJob"), Cron.Daily);

That’s it. You can see it’s very easy to use and you can integrate Hangfire very easily. Hope you like it. Stay tuned for more!

#1 for location developers in quality, price and choice, switch to HERE.

web dev ,asp.net core ,backend ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}