如何在ABP Framework中将Hangfire默认Worker Count从20改为10?
在ABP Framework中配置Hangfire后台并发作业数量
你直接配置BackgroundJobServerOptions不生效,是因为ABP框架对Hangfire做了封装,会优先使用自身的AbpHangfireOptions配置项,直接修改BackgroundJobServerOptions会被ABP的默认逻辑覆盖。
正确配置方式
方式一:通过代码配置AbpHangfireOptions
修改你的ConfigureHangfire方法,把并发数配置到AbpHangfireOptions的ServerOptions中:
public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); Configure<AbpAutoMapperOptions>(options => { options.AddMaps<DemoApplicationModule>(); }); ConfigureHangfire(context, configuration); } private void ConfigureHangfire(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddHangfire(config => { config.UseSqlServerStorage(configuration.GetConnectionString("Default")); }); // 重点:通过ABP的Hangfire选项配置并发数 Configure<AbpHangfireOptions>(options => { options.ServerOptions = new BackgroundJobServerOptions { WorkerCount = 10, // 设置并发作业数量为10 SchedulePollingInterval = TimeSpan.FromSeconds(30) }; }); context.Services.AddTransient<IBackgroundServiceManagement, BackgroundServiceManagement>(); }
方式二:通过配置文件(appsettings.json)配置
如果不想修改代码,也可以直接在appsettings.json中添加配置,ABP会自动读取并应用:
{ "AbpHangfire": { "ServerOptions": { "WorkerCount": 10, "SchedulePollingInterval": "00:00:30" } } }
原理说明
ABP在启动Hangfire后台服务器时,会从AbpHangfireOptions中获取ServerOptions来初始化BackgroundJobServer,所以直接调用Configure<BackgroundJobServerOptions>的设置不会被ABP的Hangfire集成逻辑读取,导致你的配置不生效。
内容的提问来源于stack exchange,提问作者Jerry
相关产品推荐
相关产品推荐

