4

迁移到 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()。这意味着我们的ConfigureHangfireJobsnow 失败了,因为JobStorage.Current尚未准备好。

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

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

4

2 回答 2

0

对于最新版本的 ASP.NetCore 2.1,请使用

      app.ApplicationServices.CreateScope().ServiceProvider.GetRequiredService<JobStorage>();
于 2018-08-02T01:18:47.023 回答
0

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

services.GetRequiredService<JobStorage>();

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

于 2018-07-18T13:42:49.687 回答