Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET MVC HangFire - Execute Jobs in Background using SQLServer

0.00/5 (No votes)
15 Sep 2015 1  
An easy way to perform fire-and-forget, delayed and recurring tasks inside ASP.NET applications

Introduction

This tip walks you through configuration of HangFire.

"An easy way to perform fire-and-forget, delayed and recurring tasks inside ASP.NET applications" - http://hangfire.io/

We can easily configure different types of jobs (recurring, schedule or fire and forget).

STEP 1 - Create ASP.NET Web Application

  • Open Visual Studio 2015 and create a new project of type ASP.NET Web Application.
  • On this project, I create a solution called HangFire.

  • Press OK, and a new screen will appear, with several options of template to use on our project.
  • Select the option MVC.

STEP 2 - Install Nuget

In order to use HangFire, we need to install a Nuget package.

We can install using the Package Manager Console:

  • PM> Install-Package HangFire -Version 1.4.6

Or using the Visual Studio Nuget Management:

STEP 3 - Configure Startup Class

Need to provide connection string to our database.~

using Hangfire; 
using Microsoft.Owin; 
using Owin; 
using System; 
 
[assembly: OwinStartupAttribute(typeof(HangFire.Startup))] 
namespace HangFire 
{ 
    public partial class Startup 
    { 
        public void Configuration(IAppBuilder app) 
        { 
            ConfigureAuth(app); 
 
            GlobalConfiguration.Configuration 
                .UseSqlServerStorage("connectionString"); 
 
            BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget!")); 
 
            app.UseHangfireDashboard(); 
            app.UseHangfireServer(); 
        } 
    } 
}

STEP 4 - Run

Run application using its address followed by /hangfire, like in the image below:

When we run this address for the first time, a new table will be created on our database.

Resources

Some good resources about Signal could be found here:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here