Click here to Skip to main content
15,891,633 members
Articles / Web Development / HTML
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.75/5 (9 votes)
15 Sep 2015CPOL1 min read 37.2K   38   15   3
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/ Image 1

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.

    Image 2

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

    Image 3

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:

Image 4

STEP 3 - Configure Startup Class

Need to provide connection string to our database.~

C#
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:

Image 5

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

Image 6

Resources

Some good resources about Signal could be found here:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Devscope
Portugal Portugal
I am João Sousa, and since i finish my degree I’m working in software development using Microsoft technologies.

I was awarded

Microsoft Most Valuable Professional (MVP) 2015 – .Net

My profissional profile:

Azure Developer
.NET Developer

My Certifications:

MCTS - .NET Framework - Application Development Foundation
MCTS - .NET Framework 2.0 - Windows-based Client Development
MCTS - .NET Framework 3.5 ADO.NET Applications
MCTS - .NET Framework 3.5 ASP.NET Applications
MCSD - Programming in HTML5 with JavaScript and CSS3
MCSD - Developing ASP.NET MVC 4 Web Applications
MCSD - Developing Windows Azure and Web Services
MCSA Office 365 - Managing Office 365 Identities and Requirements
MCSA Office 365 - Enabling Office 365 Services
MCSD - Implementing Microsoft Azure Infrastructure Solutions

Comments and Discussions

 
QuestionCan you obtain sqlite or firebird sample ? Pin
aykut canturk16-Dec-15 23:25
aykut canturk16-Dec-15 23:25 
GeneralGreat. Pin
Moses Machua19-Sep-15 16:56
Moses Machua19-Sep-15 16:56 
QuestionOne question Pin
Tridip Bhattacharjee15-Sep-15 21:52
professionalTridip Bhattacharjee15-Sep-15 21:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.