yuzd / Hangfire.HttpJob

httpjob for Hangfire,restful api for Hangfire,job调度与业务分离
https://github.com/yuzd/Hangfire.HttpJob/wiki
MIT License
635 stars 187 forks source link

JobAgent通过代码配置无法生效 #118

Closed bao2314483 closed 3 years ago

bao2314483 commented 3 years ago

在JobAgent示例中都是通过appsettings.json来配置,但是通过代码来配置缺无法生效 通过appsettings.json来配置是生效的

"JobAgent": {
    "Enabled": true,
    "SitemapUrl": "/jobagent",
    "EnableAutoRegister": true,
    "RegisterAgentHost": "http://localhost:5001",
    "RegisterHangfireUrl": "http://localhost:5000/job",
    "RegisterHangfireBasicName": "admin",
    "RegisterHangfireBasicPwd": "test"
  }

但是如果这样子设置就无法生效

app.UseHangfireJobAgent(AgentOpt =>
            {
                AgentOpt.Enabled(true);
                AgentOpt.WithSitemap("/jobagent");
                AgentOpt.WithEnableAutoRegister(true);
                AgentOpt.WithRegisterAgentHost("http://localhost:5001");
                AgentOpt.WithRegisterHangfireUrl("http://localhost:5000/job");
                AgentOpt.WithRegisterHangfireBasicName("admin");
                AgentOpt.WithRegisterHangfireBasicPwdd("test");
            });

我观察源码发现JobAgentServiceCollectionExtensions.AddHangfireHttpJobAgent

serviceCollection.TryAddSingleton<IConfigureOptions<JobAgentOptions>, ConfigureJobAgentOptions>();

一开始就绑定了默认配置,但是你配置信息却在UseHangfireJobAgent才可以设置,所以导致JobAgentRegisterService执行获取了配置null值. 所以应该配置信息应该在AddHangfireHttpJobAgent设置,而不是UseHangfireJobAgent在设置

yuzd commented 3 years ago

收到 我看下

bao2314483 commented 3 years ago

本来我想自己改下提交给你的,发现能力不足,改不动 你应该把JobAgentApplicationBuilderExtensions和JobAgentServiceCollectionExtensions合并一个JobAgentExtensions,这样子配置就可以共享了

yuzd commented 3 years ago

不用合并这么麻烦,采用IApplicationLifetime解决

https://github.com/yuzd/Hangfire.HttpJob/blob/245dd62328ddc0b7ffbde9c00568d2bb0c609bf9/Agent/Hangfire.HttpJob.Agent/JobAgentApplicationBuilderExtensions.cs#L70-L78

1.3.7版本 已修复