Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Configure delayed retries

Component: NServiceBus
NuGet Package: NServiceBus (8.1)

Configuring delayed retries

  • TimeIncrease: Specified as a TimeSpan, defaults to 10 seconds. 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 the default 10 seconds, i.e. 00:00:10, then the first delayed retry will be at ten seconds, the subsequent delayed retry will be 20 seconds, and so on.
  • NumberOfRetries: Number of times delayed retries are performed. Default is 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 the default ten second delay interval and sets it to five seconds.

RecoverabilityAction MyCustomRetryPolicy(RecoverabilityConfig config, ErrorContext context)
{
    var action = DefaultRecoverabilityPolicy.Invoke(config, context);
    if (!(action is DelayedRetry delayedRetryAction))
    {
        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);

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

Samples