How to start a recurring job using Hangfire in .Net Core
Sometimes our applications need to execute recurring tasks to complete an operation. For example, when you need to send a recurring notification via Email or using real-time notifications with a library like SignalR you need to perform some processes recurrently that allow them to do that. In this case, you can use the Hangfire library with it and you can register recurring jobs to solve it.
What is Hangfire ?
It’s a performance background task that can be implemented in hosted services like .NET and .NET Core applications.
For getting started using Hangfire in .Net Core you need to do the next steps :
1. Setting up the projects
Create a new project ASP.NET Core Web Application and install the package Hangfire from Nuget Package:
Or install the package via command line tools:
dotnet add package HangFire --version 1.7.12
2. Add Hangfire and configure it into .NET Core Service collection
Hangfire provide a
services.AddHangfire(); extension method that it registers all the services, their implementation that needs. In this method, we need to configure the storage where Hangfire needs to save all jobs with having two options, the first is the default package that installed when you install the Hangfire package it is
services .AddHangfire(config => config .UseSqlServerStorage(Configuration .GetConnectionString("HangfireConnection")) .UseRecurringJobAdmin(typeof(Startup).Assembly));
The second options are using the LiteDb Package, in this case, we need to install it to use
services .AddHangfire(config => config .UseLiteDbStorage(Configuration .GetConnectionString("HangfireConnection")) .UseRecurringJobAdmin(typeof(Startup).Assembly));
Install extensions HangFire.LiteDb:
dotnet add package HangFire.LiteDB --version 0.3.1
On the other hand, this extension that we are adding to the hangfire configuration
UseRecurringJobAdmin(typeof(Startup).Assembly)); It is the extensions that I created, is a simple dashboard to manage Hangfire’s recurring jobs. in my next post, I’ll talk about it. You can install all extensions whatever you want, you can see all this on this page.
Another extension of Services collection you need to add is
services.AddHangfireServer(); it registers the background task on hosted services and manages it and to perform background jobs.
Later in the method Configure of your project, you need to set up the dashboard
Your startup file it should look like this:
When you run your project it should look like this, but remember to redirect the page to
3. Add job and schedule it
If you want to schedule job you need to know What is expression CRON ?(if you want more information about it click here), it help us to schedule job by expression like that “*/5 * * * *” (it’s mean that the job run At every 5th minute).
# ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) # │ │ ┌───────────── day of the month (1 - 31) # │ │ │ ┌───────────── month (1 - 12) # │ │ │ │ ┌───────────── day of the week (0 - 6) # │ │ │ │ │ # │ │ │ │ │ # │ │ │ │ │ # * * * * * command to executeI get this information from wikipedia, it explain the expression.
you can use crontab.guru page it helps us to make expression CRON very easy:
Now in the method
Configure in the file
cs you can call RecurringJob static class, is a simple background job registration that provides by Hangfire, it allows you to add or update jobs:
RecurringJob.AddOrUpdate("JobId",() => Console.WriteLine(""),"*/5 * * * *");
Now run you project once again and done:
Check my Plugging Hangfire here.