Hangfire is an open-source software for task scheduling in ASP .NET and ASP .NET core. It uses RDBMS / No SQL storage for storing background processing job details. So, Storage connectivity is the only configuration required to implement hangfire in ASP .NET applications. This framework includes hangfire server which queries the storage to process the background job.
Prerequisite:
-Package – Hangfire.Core ,Hangfire.SqlServer ,Microsoft.Data.SqlClient
-SQL Database for background job details storage.
In program.cs, below configuration should be added.
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(builder.Configuration.GetConnectionString("localDB")));
builder.Services.AddHangfireServer().
Once the hangfire setup is configured and run below tables will be created in the mentioned database.
There are four types of background jobs in hangfire.
- FireAndForget – Occurs only once when it is created.
- RecurringJob – Occurs multiple times as mentioned in the CRON expression.
- Delayed Job – This occurs only once but on scheduled time.
- Continuation job – This calls 2 jobs one after the other based on the scheduled job.
For our demo we will see how to implement recurring job.
Use case: To send email every 1 minute.
RecurringJob.AddOrUpdate("BharathiHangfire", () => _email.SendMail(), Cron.MinuteInterval(1));
The above statement calls the function SendMail() every minute.
RecurringJob.AddOrUpdate("easyjob", () => _email.SendMail(), "0 13 * **");
The above statement calls the function SendMail() at 1PM on daily basis.
Please find the complete solution at the below link.
https://github.com/BharathiDemo/HangfireRecurringJob.git
Hope this blog helps in implementing recurring jobs using hangfire.
No Comment! Be the first one.