【问题标题】:How can I set retry count to 0 by default for all Hangfire jobs?如何将所有 Hangfire 作业的重试次数默认设置为 0?
【发布时间】:2021-06-02 02:53:00
【问题描述】:

我正在使用带有 Hangfire 的 C# ASP.NET Core 3.1 来安排后台作业。默认情况下,如果在后台作业执行过程中发生异常,则有 10 次重试尝试。我知道可以使用AutomaticRetry 属性将其设置为0,但我不想在每个后台作业中都这样做。相反,我希望默认值为 0,并且仅在我希望它为非 0 的极少数情况下使用属性手动指定它。谢谢。

【问题讨论】:

    标签: hangfire


    【解决方案1】:

    我曾经在.Net Classic中这样做过,我想在.Net Core中应该不会有太大的不同

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

    【讨论】:

    • 方法上的 AutomaticRetryAttributes 是否会覆盖此行为?
    • @Tri-EdgeAI 号
    • @Tri-EdgeAI 要实现此目标,您需要一种稍微不同的方法,您可以设置一个自定义过滤器提供程序,当且仅当不存在时才添加 AutomaticRetry 属性。见stackoverflow.com/a/70334037/1236044
    【解决方案2】:

    在 ASP.NET Core +(5, 6)... 你可以这样:

    services.AddHangfire((serviceProvider, globalConfiguration) =>
        
        // ...
        globalConfiguration.UseFilter(new AutomaticRetryAttribute { Attempts = 0 });
        // ...
    });       
    

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      相关资源
      最近更新 更多