Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • Angular js
    • Asp.net Core
    • C
    • C#
    • DotNet
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Hangfire in ASP.NET Core 3.1 – Background Jobs

Hangfire in ASP.NET Core 3.1 – Background Jobs

Date- Nov 20,2022

Updated Jan 2026

8702

Free Download Pay & Download
Hangfire in ASPNET Hangfire

Installing the Hangfire Packages

Install-Package Hangfire

Adds Hangfire service to our application. Open the Startup.cs class and locate a ConfigureServices method to register Hangfire as a service.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
            //services.AddDbContext<DbEmployeeContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddHangfireServer();
            services.AddControllers();
            services.AddSingleton<IJobTestService, JobTestService>();
        }

Adds Hangfire Dashboard to our application.

 app.UseHangfireDashboard("/mydashboard");

After setting up our local database, we need to update the appsettings.json file: Here, we register Hangfire with SQL Server. We must provide a connection string to locate the SQL Server database named DbEmployee. DefaultConnection is a connection string name, added to the appsettings.json file.

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=DESKTOP-K2Q2EAS; Database=DbEmployee; Trusted_Connection=True; MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Create DB context file 

using Microsoft.EntityFrameworkCore;

namespace hangfirecode2night.Models
{
    public class DbEmployeeContext : DbContext
    {
        public DbEmployeeContext(DbContextOptions<DbEmployeeContext> options)
            : base(options)
        {
        }

        
    }
}

we specify the IJobTestService interface where we register four different methods 

    public interface IJobTestService
    {
        void FireAndForgetJob(string userName);

        void ReccuringJob(string userName);

        void DelayedJob(string userName);

        void ContinuationJob(string userName);

    }

Our JobTestService class implements the IJobTestService interface we’ve created. Each method in the class only prints out some text to our console, instead of doing the actual work as it would in a real application.

  public class JobTestService : IJobTestService
    {
        public void FireAndForgetJob(string userName)
        {
            Console.WriteLine($"Hello from a Fire and Forget job!, {userName}");
        }

        public void ReccuringJob(string userName)
        {
            Console.WriteLine($"Hello from a Scheduled job!, {userName}");
        }

        public void DelayedJob(string userName)
        {
            Console.WriteLine($"Hello from a Delayed job!, {userName}");
        }

        public void ContinuationJob(string userName)
        {
            Console.WriteLine($"Hello from a Continuation job!, {userName}");
        }

    }

We create the ProductController constructor and inject the IBackgroundJobClient interface that Hangfire provides. We will be using this interface’s methods for scheduling different types of jobs. Besides that, we also inject the IJobTestService interface that we created.

    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly DbEmployeeContext _context;
        private readonly IDistributedCache _cache;
        private readonly IJobTestService _jobTestService;
        private readonly IBackgroundJobClient _backgroundJobClient;
        private readonly IRecurringJobManager _recurringJobManager;
        public ProductController(IJobTestService jobTestService, IBackgroundJobClient backgroundJobClient,
            IRecurringJobManager recurringJobManager)
        {
            _jobTestService = jobTestService;
            _backgroundJobClient = backgroundJobClient;
            _recurringJobManager = recurringJobManager;
        }
    }

Fire-and-forget Jobs

Fire-and-forget jobs are executed only once and almost immediately after creation. We will create our first background Job. Open up the Hangfire Controller that we had created. We will create a POST endpoint that welcomes a user with an email (ideally). Add in these codes.

        //FireAndForgetJob
        [HttpGet("/FireAndForgetJob")]
        public ActionResult CreateFireAndForgetJob(string userName)
        {
            var jobId = _backgroundJobClient.Enqueue(() => _jobTestService.FireAndForgetJob(userName));
            return Ok($"Job Id {jobId} Completed. Welcome Mail Sent!");
        }

Delayed Jobs

Delayed tasks are those we surely want to execute, but just not right now. We can schedule them at a certain time, maybe a minute from now or three months from now.

Now, what if we want to send a mail to a user, not immediately, but after 10 mins In such cases, we use delayed Jobs. Let’s see its implementation after which I will explain it in detail. In the same controller, add these lines of code. It is quite similar to the previous variant, but we introduce a delay factor to it.

        //DelayedJob
        [HttpGet("/DelayedJob")]
        public ActionResult CreateDelayedJob(string userName)
        {
            var jobId = _backgroundJobClient.Schedule(() => _jobTestService.DelayedJob(userName), TimeSpan.FromSeconds(60));
            return Ok($"Job Id {jobId} Completed. Delayed Welcome Mail Sent!");

        }

Recurring Jobs

We schedule our recurring jobs so they can repeat in a certain interval. For those types of tasks, Hangfire makes use of the CRON software utility.

Our Customer has a subscription to our service. We would obviously have to send him/her a reminder about payment or the invoice itself. This calls the need for a Recurring Job, where I can send my customer emails on a monthly basis. This is supported in Hangfire using the CRON schedule.

What is CRON? CRON is a time-based utility that can define time intervals. Let’s see how to achieve such a requirement.

        //ReccuringJob
        [HttpGet("/ReccuringJob")]
        public ActionResult CreateReccuringJob(string userName)
        {
            _recurringJobManager.AddOrUpdate("jobId", () => _jobTestService.ReccuringJob(userName), Cron.Minutely);
            return Ok($"Recurring Job Scheduled. Invoice will be mailed Monthly for {userName}!");
        }

Continuation Jobs

The last type of job we are going to cover is the Continuation job. Its main feature is that it chains together task execution. With it, we can get two jobs to run one after the other in continuation.

This is a more complicated scenario. Let me try to keep it very simple. A user decides to unsubscribe from your service. After he confirms his action (Maybe by clicking the unsubscribe button), we (the application) have to unsubscribe him from the system and send him a confirmation mail after that as well. So, the First job is to actually unsubscribe the user. The second job is to send a mail confirming the action. The second job should be executed only after the first job is completed properly. Get the scenario?

        //ContinuationJob
        [HttpGet("/ContinuationJob")]
        public ActionResult CreateContinuationJob(string userName)
        {
            var jobId = _backgroundJobClient.Enqueue(() => _jobTestService.FireAndForgetJob(userName));
            _backgroundJobClient.ContinueJobWith(jobId, () => _jobTestService.ContinuationJob(userName));

            return Ok($"Unsubscribed");
        }

you can copy all the controller's code 

using Hangfire;
using hangfirecode2night.Models;
using hangfirecode2night.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace hangfirecode2night.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly DbEmployeeContext _context;
        private readonly IDistributedCache _cache;
        private readonly IJobTestService _jobTestService;
        private readonly IBackgroundJobClient _backgroundJobClient;
        private readonly IRecurringJobManager _recurringJobManager;
        public ProductController(IJobTestService jobTestService, IBackgroundJobClient backgroundJobClient,
            IRecurringJobManager recurringJobManager)
        {
            _jobTestService = jobTestService;
            _backgroundJobClient = backgroundJobClient;
            _recurringJobManager = recurringJobManager;
        }

        //FireAndForgetJob
        [HttpGet("/FireAndForgetJob")]
        public ActionResult CreateFireAndForgetJob(string userName)
        {
            var jobId = _backgroundJobClient.Enqueue(() => _jobTestService.FireAndForgetJob(userName));
            return Ok($"Job Id {jobId} Completed. Welcome Mail Sent!");
        }

        //DelayedJob
        [HttpGet("/DelayedJob")]
        public ActionResult CreateDelayedJob(string userName)
        {
            var jobId = _backgroundJobClient.Schedule(() => _jobTestService.DelayedJob(userName), TimeSpan.FromSeconds(60));
            return Ok($"Job Id {jobId} Completed. Delayed Welcome Mail Sent!");

        }

        //ReccuringJob
        [HttpGet("/ReccuringJob")]
        public ActionResult CreateReccuringJob(string userName)
        {
            _recurringJobManager.AddOrUpdate("jobId", () => _jobTestService.ReccuringJob(userName), Cron.Minutely);
            return Ok($"Recurring Job Scheduled. Invoice will be mailed Monthly for {userName}!");
        }

        //ContinuationJob
        [HttpGet("/ContinuationJob")]
        public ActionResult CreateContinuationJob(string userName)
        {
            var jobId = _backgroundJobClient.Enqueue(() => _jobTestService.FireAndForgetJob(userName));
            _backgroundJobClient.ContinueJobWith(jobId, () => _jobTestService.ContinuationJob(userName));

            return Ok($"Unsubscribed");
        }
    }
}

Let’s fire up the application and go to Postman.

https://localhost:44305/FireAndForgetJob?userName=Shubham Batra

Let’s fire up the application and go to Postman.

https://localhost:44305/DelayedJob?userName=Shubham Batra

Let’s fire up the application and go to Postman.

https://localhost:44305/ReccuringJob?userName=Shubham Batra

Let’s fire up the application and go to Postman.

https://localhost:44305/ContinuationJob?userName=Shubham Batra

Check your database for all hangfire transaction


S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

MVC Crud Operation With Entity Framework
Sep 20, 2020
Get random number in asp.net C#
Dec 23, 2023
DataReader in ADO.NET
May 31, 2023
Mastering Async and Await in C#: A Comprehensive Guide
Mar 16, 2026
Previous in ASP.NET Core
Payout using Paypal Payment Gateway
Next in ASP.NET Core
Web Api in Asp.net core

Comments

Contents

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 25964 views
  • Exception Handling Asp.Net Core 20734 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20210 views
  • How to implement Paypal in Asp.Net Core 19624 views
  • Task Scheduler in Asp.Net core 17516 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor