Hangfire Background Delayed Jobs — Console Application

Ghanshyam Shukla
3 min readDec 19, 2020

--

In one of my previous article, I wrote about Hangfire Recurring Job.

In this article, I am going to explain the working of Hangfire Delayed Jobs

Hangfire is an open-source framework that helps you to create, process and manage your background jobs, i.e. operations you don’t want to put in your request processing pipeline:

https://www.hangfire.io/

Hangfire supports different types of background jobs:

Fire-and-forget, Background Process, Delayed, Continuations, Batches, Batch Continuations

Delayed jobs are executed only once too, but not immediately — only after the specified time interval.

Install the Hangfire package using NuGet

PM> Install-Package Hangfire

In the App.config, add the connecting string to get Hangfire Job Storage as:

<add name=”HangfireConnection” connectionString=”Data Source=GHANSHYAMS\SQLEXPRESS;Initial Catalog=Hangfire_Demo;Integrated Security=True;” providerName=”System.Data.SqlClient”/>

Now, install Topshelf package from NuGet and also some more packages like Microsoft.Owin.Host.HttpListener, Microsoft.Owin.Host.SystemWeb & Microsoft.Owin.Hosting

Add the Startup.cs class and configure the Hangfire dashboard.

Delayed Jobs

The Delayed Job structure consists of Method/Function to execute, Queue, Type, Delay Time Span

By default Hangfire processes Default queue, but you can also specify your own queue like:

Queue("feedback")]
public void SomeMethod() { }

To begin processing queues, you need to update your BackgroundJobServer configuration.

Methods to Trigger by Hangfire

var options = new BackgroundJobServerOptions
{
Queues = new[] { "feedback", "default" }
};
app.UseHangfireServer(options);

Method to Trigger

After all configurations are done, start the console application and open the below URL in browser as:

You can access Hangfire dashboard with “/hangfire”.

Output

Hangfire References

GitHub Project Reference

Happy Learning

--

--

Ghanshyam Shukla
Ghanshyam Shukla

Written by Ghanshyam Shukla

Consultant — Software Development

Responses (1)