有没有办法在 Asp.NetCore 控制台应用程序的 "ConfigureServices" 方法中设置队列监听 hangfire?

Is there a way to setup queue listening for hangfire in the "ConfigureServices" method in an Asp.NetCore console application?

使用 Asp.NetCore 的主机生成器,我正在尝试使用我可以在 "ConfigureServices" 方法中使用的方法更改 Hangfire 的监听队列。

我想知道我是否可以这样做,或者我是否有义务使用 :

using (new BackgroundJobServer(options)) { /* ... */ } 来自 The documentation 或者如果有其他方法。

这是我的主要方法

static void Main(string[] args)
{
    HostBuilder hostBuilder = new HostBuilder();
    hostBuilder.ConfigureServices(ConfigureServices);
    hostBuilder.Build().Run();
}

这是我的 ConfigureServices 方法的样子:

public static void ConfigureServices(IServiceCollection services)
{
    services.AddHangfire(config =>
    {
        config.UsePostgreSqlStorage();
    });

    services.AddHangfireServer();
}

我希望 AddHangfireServer 有一个接受 BackgroundJobServerOptions 的重载,但我没有找到。

有没有办法让我错过一个过载或者你是否完全以另一种方式设置监听队列?

编辑:从 Hangfire 1.7.5 (As seen here)

开始,services.AddHangfireServer(); 将有一个过载接受 BackgroundJobServerOptions

hangfire 1.7.5以下版本的回复:

我查看了 AddHangfireServer 方法的 code,他们正在做 :

var options = provider.GetService<BackgroundJobServerOptions>() ?? new BackgroundJobServerOptions();

因此传递 BackgroundJobServerOptions 的方法是在调用 AddHangfireServer 方法之前将其注册到 IoC 容器。

这是我最后的 ConfigureServices 方法:

public static void ConfigureServices(IServiceCollection services)
{

    //this was added
    services.AddSingleton(new BackgroundJobServerOptions()
    {
        //you can change your options here
        Queues = new[] { "etl" }
    });

    services.AddHangfire(config =>
    {
        config.UsePostgreSqlStorage();
    });

    services.AddHangfireServer();
}