在 .Net 5 的工作服务中配置 Hangfire 仪表板?

发布于 2025-01-14 21:10:02 字数 1573 浏览 1 评论 0原文

我正在使用 Hangfire 来安排我的工作人员服务中的作业,并希望使用 Hangfire 仪表板。但似乎没有办法配置这个。所有文档都使用 Startup 类,但我的工作服务中没有任何启动。另外,.Net 5 不支持 OWIN NuGet 包。这是我尝试过的,

            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 仪表板。

I am using Hangfire to schedule jobs in my worker service and want to use the hangfire dashboard. But it seems that there is no way to configure this. All the documentation uses the Startup class but I don't have any startup in my worker service. Also, the OWIN NuGet package is not supported in .Net 5. Here is what I've tried,

            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();

and I've configured hangfire like this,

                 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();

Please note that I am able to schedule and execute jobs by hangfire in the current implementation, all I need now is to configure the hangfire dashboard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹过旳痕迹 2025-01-21 21:10:02

使用以下 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 上提供。

Use the following Program.cs to configure Hangfire dashboard and your worker service:

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 dashboard will be available at http://localhost:5000/hangfire.

不知在何时 2025-01-21 21:10:02

在某些情况下只有 app.UseHangfireDashboard(); 已经完成了这项工作(启用 Hangfire Dashboard)。

.NET 6 Program.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();

In some cases only the app.UseHangfireDashboard(); already do the job(enables Hangfire Dashboard).

.NET 6 Program.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();
王权女流氓 2025-01-21 21:10:02

如果没有 using Microsoft.AspNetCore.Hosting;,此示例将无法工作;感谢问题中的 LMio [https://stackoverflow.com/questions/72828395/hangfire-server-in-net-worker-service]

This example does not work without using Microsoft.AspNetCore.Hosting; Thanks to LMio from question[ https://stackoverflow.com/questions/72828395/hangfire-server-in-net-worker-service]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文