簡體   English   中英

在 .Net 5 的工作人員服務中配置 Hangfire 儀表板?

[英]Configure Hangfire dashboard in a worker service in .Net 5?

我正在使用 Hangfire 來安排我的工作人員服務中的作業,並想使用 hangfire 儀表板。 但是好像沒有辦法配置這個。 所有文檔都使用 Startup class 但我的工作人員服務中沒有任何啟動。 此外,.Net 5 不支持 OWIN NuGet package。這是我嘗試過的,

            var hostBuilder = CreateHostBuilder(args)
                .Build();

            var services = hostBuilder.Services;
            var applicationBuilder = new ApplicationBuilder(services);

            applicationBuilder.UseRouting();

            applicationBuilder.UseHangfireDashboard("/hangfire");

            applicationBuilder.UseEndpoints(endpoints =>
            {
                endpoints.MapHangfireDashboard();
            });

            hostBuilder.Run();

我已經像這樣配置了 hangfire,

                 services.AddHangfire(configuration => configuration
                    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                    .UseSimpleAssemblyNameTypeSerializer()
                    .UseRecommendedSerializerSettings()
                    .UseSqlServerStorage("connection string",
                    {
                        CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                        SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                        QueuePollInterval = TimeSpan.Zero,
                        UseRecommendedIsolationLevel = true,
                        DisableGlobalLocks = true
                    }));

                // Add the processing server as IHostedService
                services.AddHangfireServer();

請注意,在當前的實現中,我可以通過 hangfire 安排和執行作業,我現在需要的只是配置 hangfire 儀表板。

使用以下Program.cs配置 Hangfire 儀表板和您的工作人員服務:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args)
            .Build()
            .Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(builder =>
            {
                builder.Configure(app =>
                {
                    app.UseRouting();

                    app.UseHangfireDashboard();
                    app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapHangfireDashboard();
                    });
                });
            })
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHangfire(conf => conf.UseSqlServerStorage("connection string"));
                services.AddHangfireServer();

                // your worker service
                services.AddHostedService<Worker>();
            });
}

Hangfire 儀表板將在http://localhost:5000/hangfire提供。

如果不using Microsoft.AspNetCore.Hosting; 感謝來自問題的LMio [ https://stackoverflow.com/questions/72828395/hangfire-server-in-net-worker-service]

在某些情況下,只有app.UseHangfireDashboard(); 已經完成工作(啟用 Hangfire 儀表板)。

Program.cs 6 程序.cs

using Hangfire;
using Microsoft.AspNetCore.Hosting;

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(builder =>
    {
        builder.Configure(app => { app.UseHangfireDashboard(); });
    })
    .ConfigureServices(services =>
    {
        services.AddHostedService<Worker>();

        services.AddHangfire(x => x.UseSqlServerStorage("<connection string>"));
    })
    .Build();

await host.RunAsync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM