Moving to asp.net core 2.1 is breaking our hangfire job setup.
In the Main
method of Program.cs
we have something like
var webHost = BuildWebHost(args);
ConfigureHangfireJobs(webHost);
webHost.Run();
The BuildWebHost
looks like this:
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ConfigConfiguration)
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
ConfigureHangfireJobs
is calling RecurringJob.AddOrUpdate()
which needs the JobStorage.Current
to be set, and I found it gets set when the hangfireserver middleware is added, while the hangfire service is configured in the Startup.ConfigureServices
method. As far as I know, middleware setup is to be done in the Configure
method of the Startup
class, so that's where we call the app.UseHangfireServer
which sets the JobStorage.Current
.
With core 2.0 everything was running fine as the Configure
method was called during our BuildWebHost()
method and so the JobStorage.Current
was then setup and available for the ConfigureHangfireJobs
. But when switching to core 2.1, the Configure
is now called as part of the webHost.Run()
method. Which means our ConfigureHangfireJobs
now fails because the JobStorage.Current
is not ready yet.
Now, of course I could setup the hangfire jobs as part of the Startup.Configure
method, but that's just not where it belongs. An other option would be to setup the JobStorage.Current
myself, but then isn't that the responsibility of the hangfireserver middleware ?
So my question is: how are we supposed to setup hangfire jobs correctly in core 2.1 ?
Ok, I got it working by requesting the JobStorage service like so:
services.GetRequiredService<JobStorage>();
Even if I don't need a hand on it, it sets the JobStorage.Current.