Configure delayed retries

Component: NServiceBus
NuGet Package NServiceBus (7-pre)
This page targets a pre-release version and is subject to change prior to the final release.
Starting from NServiceBus Version 6 Delayed Retries Policy (formerly known as Second Level Retries Policy) has been deprecated in favor of the new custom Recoverability policy which allows much more control over the Recoverability behavior. This documentation shows how previous Delayed Retries Policies can be implemented with the new Recoverability Policy.
Delayed Retries cannot be used when transport transactions are disabled or Delayed Delivery is not available. For more information about transport transactions, refer to transport transaction. For more details on the caveats, see: Delayed Delivery article.

Configuring Delayed Retries

  • TimeIncrease: Specified as a TimeSpan. Specifies the delay interval for each retry attempt. This delay increases by the same timespan with each delayed delivery. For example, if the specified value is 10 seconds, i.e. 00:00:10, then the first delayed retry will be at 10 seconds, the subsequent delayed retry will be 20 seconds and so on.
  • NumberOfRetries: Number of times Delayed Retries are performed. Default: 3.
var recoverability = endpointConfiguration.Recoverability();
recoverability.Delayed(
    delayed =>
    {
        delayed.NumberOfRetries(2);
        delayed.TimeIncrease(TimeSpan.FromMinutes(5));
    });

Disabling through code

var recoverability = endpointConfiguration.Recoverability();
recoverability.Delayed(
    delayed =>
    {
        delayed.NumberOfRetries(0);
    });

Custom Retry Policy

Custom retry logic can be configured via code.

var recoverability = endpointConfiguration.Recoverability();
recoverability.CustomPolicy(MyCustomRetryPolicy);

Simple Policy

The following retry policy overrides default 10 seconds delay interval and sets it to 5 seconds.

RecoverabilityAction MyCustomRetryPolicy(RecoverabilityConfig config, ErrorContext context)
{
    var action = DefaultRecoverabilityPolicy.Invoke(config, context);
    var delayedRetryAction = action as DelayedRetry;
    if (delayedRetryAction == null)
    {
        return action;
    }
    // Override default delivery delay.
    return RecoverabilityAction.DelayedRetry(TimeSpan.FromSeconds(5));
}

Exception based Policy

Sometimes the number of retries or the delay interval might depend on the error exception thrown. The following retry policy extends the previous one by skipping delayed retries whenever MyBusinessException has been thrown during incoming message processing.

RecoverabilityAction MyCustomRetryPolicy(RecoverabilityConfig config, ErrorContext context)
{
    var action = DefaultRecoverabilityPolicy.Invoke(config, context);

    var delayedRetryAction = action as DelayedRetry;
    if (delayedRetryAction == null)
    {
        return action;
    }
    if (context.Exception is MyBusinessException)
    {
        return RecoverabilityAction.MoveToError(config.Failed.ErrorQueue);
    }
    // Override default delivery delay.
    return RecoverabilityAction.DelayedRetry(TimeSpan.FromSeconds(5));
}

Samples


Last modified