You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hangfire 1.7.34:升级后仪表盘手动触发定时任务按钮消失如何恢复?

解决Hangfire 1.7.34版本仪表盘“立即触发”按钮消失的问题

问题原因

升级到Hangfire 1.7.34后,仪表盘默认配置可能开启了只读模式,或缺少必要的授权规则,导致“立即触发”交互按钮被隐藏;若未显式配置仪表盘选项,部分功能会被默认禁用。

解决方案

1. 配置仪表盘权限并关闭只读模式

在项目的Startup.cs(.NET Core 3.x及更早)或Program.cs(.NET 6+)的中间件配置阶段,添加Hangfire仪表盘的显式配置,开启交互权限:

// .NET Core 3.x 及更早版本 Startup.Configure 方法
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // 其他中间件配置...

    // 配置Hangfire仪表盘
    app.UseHangfireDashboard("/hangfire", new DashboardOptions
    {
        // 开发环境允许所有访问,生产环境建议替换为自定义授权过滤器
        Authorization = new[] { new AllowAllAuthorizationFilter() },
        // 关闭只读模式,启用所有交互功能(含立即触发按钮)
        EnableReadOnlyMode = false
    });

    app.UseHangfireServer();
}

// .NET 6+ Program.cs 写法
var app = builder.Build();

// 其他中间件配置...

app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
    Authorization = new[] { new AllowAllAuthorizationFilter() },
    EnableReadOnlyMode = false
});

app.UseHangfireServer();

2. 优化RecurringJob注册逻辑

你现有代码在服务配置阶段注册循环任务的方式可行,但建议将任务注册移至应用启动阶段,避免潜在的服务初始化问题:

// 修改HangfireHelper类
public static class HangfireHelper
{
    public static void AddHangfire(IServiceCollection services, string connectionString)
    {
        services.AddHangfire(x =>
        {
            x.UsePostgreSqlStorage(connectionString);
        });

        services.AddHangfireServer(ops =>
        {
            ops.ServerName = "my job server";
            ops.WorkerCount = 10;                
            ops.Queues = new string[] { "general" };
            ops.SchedulePollingInterval = TimeSpan.FromSeconds(10);
            ops.CancellationCheckInterval = TimeSpan.FromSeconds(10);
        });
    }

    // 单独提取循环任务注册方法
    public static void RegisterRecurringJobs(string recurrence)
    {
        RecurringJob.AddOrUpdate<MyClass>(e => e.MyMethodToExecute(), recurrence);
    }
}

// 在Startup.Configure或Program.cs中调用注册方法
HangfireHelper.RegisterRecurringJobs("你的定时表达式");

验证效果

重启应用后访问Hangfire仪表盘(默认路径/hangfire),找到目标循环任务,即可看到“Trigger Now”(立即触发)按钮恢复显示。

内容的提问来源于stack exchange,提问作者Ali CAKIL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:34:55