How to use Hangfire with new worker in .NET core 3?

aspnetcore
recurring
Tags: #<Tag:0x00007f49ac031900> #<Tag:0x00007f49ac0317c0>

#1

When using new worker service

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHangfireServer();
services.AddHangfire(config => config.UseSqlServerStorage(“connectionString”));
services.AddHostedService();
});

  1. How do I pass in BackgroundJobServerOptions?

  2. How can I queue all my recurring jobs in this setup? I can do it by calling GlobalConfiguration.Configuration.UseSqlServerStorage and RecurringJob.AddOrUpdate but I’d like to avoid calling static methods. Can it be done?


#2
  1. I you want to pass BackgroundJobServerOptions, you need to inject it before calling AddHangfireServer(); (see : https://stackoverflow.com/questions/56112793/is-there-a-way-to-setup-queue-listening-for-hangfire-in-the-configureservices/56112933#56112933)

  2. If you don’t want to use RecurringJob.AddOrUpdate, you can use the class RecurringJobManager. Here’s the documentation about recurrent Tasks