dotnet add package Hangfire.AspNetCore
dotnet add package Hangfire.Core
dotnet add package Hangfire.SqlServer
"ConnectionStrings": {
"HangfireConnection": "Server=(localdb)\\MSSqlLocalDb;Database=Hangfire_DB;Integrated Security=SSPI;"
},
Now You Can Access To Hangfire Dashboard In Address "http://localhost:0000/hangfire"
#region [ Hangfire Services ]
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
{
TryAutoDetectSchemaDependentOptions = false
})
.UseLogProvider(new CustomLogProvider()));
builder.Services.AddHangfireServer();
#endregion [ Hangfire Services ]
var app = builder.Build();
#region [ Hangfire ]
//var options = new BackgroundJobServerOptions
//{
// ServerName = String.Format(
// "{0}.{1}",
// Environment.MachineName,
// Guid.NewGuid().ToString())
//};
//app.UseHangfireServer(options);
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
//IsReadOnlyFunc = (DashboardContext context) => true
});
#endregion [ Hangfire ]
public async Task EnqueueJob()
{
string jobId =
BackgroundJob.Enqueue("calculatefeebyenqueuejob_enqueue_type",
() => _payrollWrapper.CalculatePayrollAsync());
await Task.Delay(1);
}
public async Task ScheduleJob()
{
string jobId =
BackgroundJob.Schedule("calculatefeebyschedulejob_schedule_type",
() => _payrollWrapper.CalculatePayrollAsync(),
TimeSpan.FromSeconds(10));
await Task.Delay(1);
}
You Can Fix Daily From "Cron.Daily" And "Cron.Yearly()" And "string cronExp = "* * */8 * *";" Like This
public async Task RecurringJob()
{
//Cron.Daily
//Cron.Yearly()
//string cronExp = "* * */8 * *";
Hangfire.RecurringJob.AddOrUpdate("calculatefeebyrecurring_addorupdate_type",
() => _payrollWrapper.CalculatePayrollAsync(),
Cron.Daily);
await Task.Delay(1);
}
public async Task RecurringRemoveIfExists()
{
using (var connection = JobStorage.Current.GetConnection())
{
//JobData jobData13 = connection.GetJobData("29");
//BackgroundJob.Delete("29");
foreach (var recurringJob in connection.GetRecurringJobs())
{
Hangfire.RecurringJob.RemoveIfExists(recurringJob.Id);
}
}
await Task.Delay(1);
}