如何将Hangfire作业的重试计数默认设置为0?

3
我正在使用C# ASP.NET Core 3.1和Hangfire来安排后台作业。默认情况下,在执行后台作业时,如果发生异常,将进行10次重试尝试。我知道可以使用AutomaticRetry属性将其设置为0,但我不想在每个后台作业上都这样做。相反,我希望默认值为0,并且仅在非常罕见的情况下手动指定该属性才会使其变为非0。谢谢。
2个回答

5

我以前在 .Net Classic 中是这样做的,我想在 .Net Core 中应该没什么不同。

// disable automatic retry if a job fails
Hangfire.GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute() { Attempts = 0 }); 

但是,在方法上使用AutomaticRetryAttributes会覆盖这种行为吗? - Tri-Edge AI
@Tri-EdgeAI 不。 - Igor
为了实现这个目标,您需要采用略微不同的方法,设置一个自定义过滤器提供程序,仅在不存在AutomaticRetry属性时添加它。请参见https://dev59.com/VK72oIgBc1ULPQZFvFdM#70334037。 - jbl

3
在ASP.NET Core +(5, 6)上,您可以做到这一点:
services.AddHangfire((serviceProvider, globalConfiguration) =>
    
    // ...
    globalConfiguration.UseFilter(new AutomaticRetryAttribute { Attempts = 0 });
    // ...
});       

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接