在 Hangfire 中配置自动重试属性筛选器的位置



我们正在使用Hangfire,失败的作业应该重试。我们使用以下方法来指定重试次数:

GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 3 });

我们的问题是:我们必须在哪里配置此过滤器?在将项目放入队列的程序还是在处理队列项目的处理服务?

对于 .NET Core 7.0

 // Add Hangfire services.
    builder.Services.AddHangfire(x=> x
            ..........................................
            ..........................................
            ..........................................
            ..........................................
            ).UseFilter(new AutomaticRetryAttribute 
            { 
                Attempts = 5, 
                LogEvents = true, 
                OnAttemptsExceeded = AttemptsExceededAction.Fail, 
                DelaysInSeconds = new int[5] { 1, 2, 3}
            })

您在处理服务中配置过滤器,它与文档中所述的 per 方法属性相同

希望这个解决方案可以帮助那些仍在 Hangfire 重试流程上寻找解决方案的人。如果您使用的是 .Net6 然后在程序中.cs或如果您使用的是 .NetCore5 或更早的版本,然后在启动中.cs在"配置 HTTP 请求管道部分"中

.app。UseHangfireDashboard((;//在此行下方添加以下全局过滤器GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Try = 3, DelaysInSeconds = new int[] { 1200 } }(;其中,尝试次数 = 3 次失败时重试三次,延迟秒数是 20 分钟后重试的持续时间间隔 = 60*20 = 1200 秒。

最新更新