首页 > 解决方案 > 多台服务器在 HANGFIRE 中生成错误 500

问题描述

当我使用 pod 管理的 Kubernetes 架构将应用程序投入生产时,它具有扩展的可能性,因此今天它有两台服务器运行相同的应用程序,hangfire 识别两者但返回错误 500

Unable to refresh the statistics: the server responded with 500 (error). Try reloading the page manually, or wait for automatic reload that will happen in a minute.

但是当我离开只有一台服务器的测试应用程序的舞台时,hangfire 工作正常。

挂火配置:

Startup.cs

services.AddHangfire(x => x.UsePostgreSqlStorage(Configuration.GetConnectionString("DefaultConnection")));


app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
   Authorization = new[] { new AuthorizationFilterHangfire() }
});           
app.UseHangfireServer();

错误

标签: c#asp.net-corekuberneteshangfire

解决方案


您现在可以添加IgnoreAntiforgeryToken到您的服务中,这应该可以解决此问题。

根据this github post,当您有多个服务器运行仪表板时会出现问题,并且由于负载平衡,当您的请求与最初获得页面的服务器不同时,您会看到错误。

添加IgnoreAntiforgeryToken = true到仪表板应该可以解决问题。

摘自这里

app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
    Authorization = new[] {new HangfireAuthFilter()},
     IgnoreAntiforgeryToken = true                                 // <--This
});

推荐阅读