在 ASPNET Core 2.1 应用程序中正确设置 hangfire 作业?

Setup hangfire jobs correctly within an ASPNET Core 2.1 application?

迁移到 asp.net 核心 2.1 会破坏我们的 hangfire 作业设置。

Program.csMain 方法中,我们有类似

的东西
var webHost = BuildWebHost(args);
ConfigureHangfireJobs(webHost);
webHost.Run();

BuildWebHost 看起来像这样:

return WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration(ConfigConfiguration)
    .UseStartup<Startup>()
    .UseApplicationInsights()
    .Build();

ConfigureHangfireJobs 正在调用 RecurringJob.AddOrUpdate() 需要设置 JobStorage.Current ,我发现它是在添加 hangfireserver 中间件时设置的,而 hangfire 服务是在Startup.ConfigureServices 方法。据我所知,中间件设置是在 Startup class 的 Configure 方法中完成的,所以这就是我们调用 app.UseHangfireServer 设置 [=17] 的地方=].

对于核心 2.0,一切都 运行 很好,因为 Configure 方法在我们的 BuildWebHost() 方法中被调用,因此 JobStorage.Current 然后被设置并可用于 ConfigureHangfireJobs。但是当切换到核心 2.1 时,Configure 现在作为 webHost.Run() 方法的一部分被调用。这意味着我们的 ConfigureHangfireJobs 现在失败了,因为 JobStorage.Current 还没有准备好。

现在,我当然可以将 hangfire 作业设置为 Startup.Configure 方法的一部分,但这不是它所属的位置。另一种选择是我自己设置 JobStorage.Current,但这不是 hangfireserver 中间件的责任吗?

所以我的问题是:我们应该如何在核心 2.1 中正确设置 hangfire 作业?

好的,我通过像这样请求 JobStorage 服务来让它工作:

services.GetRequiredService<JobStorage>();

即使我不需要动手,它也会设置 JobStorage.Current。

最新版本ASP.NetCore 2.1 使用

      app.ApplicationServices.CreateScope().ServiceProvider.GetRequiredService<JobStorage>();