I am trying to setup recurring jobs at startup in my .NET Core web app but I get this error:
System.InvalidOperationException: ‘JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API.’
Here is the code I am using in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IPrincipal>(
provider => provider.GetService<IHttpContextAccessor>().HttpContext?.User);
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AppSettings>(Configuration.GetSection("appSettings"));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton(Configuration);
services.AddTransient<AppSettings, AppSettings>();
services.AddTransient<IAdminBO, AdminBO>();
services.AddHangfire(config => config.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
var sp = services.BuildServiceProvider();
var adminBO = sp.GetService<IAdminBO>();
RecurringJob.AddOrUpdate("ConfirmReminder", () => adminBO.SendRemindersConfirmAccount(), "0 5 * * 0"); // every sunday at 05:00
services.AddMvc();
//Following line is only required if your Hangfire jobs are failing.
//services.AddTransient<HomeController, HomeController>();
}