Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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.

new-aspnet-core-application-hangfire

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

aspnet-core-hang-fire-web-application


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


install-hangfire-nuget-package


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.


hangfire-sample-database


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.
    services.AddMvc();
    services.AddHangfire(config=>config.UseSqlServerStorage(Configuration.GetConnectionString("HangFireConnectionString")));
}

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.


hangfire-tables-sql-server


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!

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

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

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}