迁移到 asp.net core 2.1 破坏了我们的 hangfire 作业设置。
在我们的Main
方法中,Program.cs
我们有类似的东西
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中间件时设置的,而在Startup.ConfigureServices
方法中配置了hangfire服务。据我所知,中间件设置是在类的Configure
方法中完成的Startup
,所以我们称之为app.UseHangfireServer
设置JobStorage.Current
.
在核心 2.0 中,一切都运行良好,因为Configure
在我们的方法期间调用了该BuildWebHost()
方法,因此JobStorage.Current
随后设置并可用于ConfigureHangfireJobs
. 但是当切换到核心 2.1 时,Configure
现在将作为方法的一部分调用webHost.Run()
。这意味着我们的ConfigureHangfireJobs
now 失败了,因为JobStorage.Current
尚未准备好。
现在,我当然可以将hangfire 作业设置为该Startup.Configure
方法的一部分,但这不属于它。另一种选择是设置JobStorage.Current
我自己,但这不是 hangfireserver 中间件的责任吗?
所以我的问题是:我们应该如何在 core 2.1 中正确设置 hangfire 作业?