What is Hangfire?
Hangfire is an open source that schedules background jobs with simple implementation in .Net core. It is a reliable and persistent system with a monitoring facility.

How to use it?
We can download the Hangfire package by running the following command in the Package Manager console,
PM> Install-Package Hangfire
2. Add Hangfire into the global configuration,
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("Database=Hangfire.Sample; Integrated Security=True;", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true
})
.UseBatches()
.UsePerformanceCounters();
3. We need to provide necessary background task settings and SQL DB (we can use Redis and InMemory as well) connectivity in the program.cs, and it will create its tables in SQL.
using System;
using Hangfire;
using Hangfire.SqlServer;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseColouredConsoleLogProvider()
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("Database=Hangfire.Sample; Integrated Security=True;", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true
});
BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));
using (var server = new BackgroundJobServer())
{
Console.ReadLine();
}
}
}
}

Join the conversation! Your thoughts help the community grow.