App-vNext/Polly · error · ArgumentNullException

onRetry

Error message

onRetry

What it means

Thrown by RetryAsync<TResult> (result-typed retry policy) when the synchronous Action<DelegateResult<TResult>, int> onRetry callback is null. This overload wraps the Action into an async delegate, but still null-checks the Action first; the callback surfaces the handled result/exception and retry count on each retry. Construction fails eagerly because a null callback cannot run.

Source

Thrown at src/Polly/Retry/AsyncRetryTResultSyntax.cs:68

    public static AsyncRetryPolicy<TResult> RetryAsync<TResult>(this PolicyBuilder<TResult> policyBuilder, Func<DelegateResult<TResult>, int, Task> onRetryAsync) =>
        policyBuilder.RetryAsync(1, onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i));

    /// <summary>
    ///     Builds an <see cref="AsyncRetryPolicy{TResult}" /> that will retry <paramref name="retryCount" /> times
    ///     calling <paramref name="onRetry" /> on each retry with the handled exception or result and retry count.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="policyBuilder">The policy builder.</param>
    /// <param name="retryCount">The retry count.</param>
    /// <param name="onRetry">The action to call on each retry.</param>
    /// <returns>The policy instance.</returns>
    /// <exception cref="ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="onRetry"/> is <see langword="null"/>.</exception>
    public static AsyncRetryPolicy<TResult> RetryAsync<TResult>(this PolicyBuilder<TResult> policyBuilder, int retryCount, Action<DelegateResult<TResult>, int> onRetry)
    {
        if (onRetry == null)
        {
            throw new ArgumentNullException(nameof(onRetry));
        }

#pragma warning disable 1998 // async method has no awaits, will run synchronously
        return policyBuilder.RetryAsync(retryCount,
            onRetryAsync: async (outcome, i, _) => onRetry(outcome, i));
#pragma warning restore 1998
    }

    /// <summary>
    ///     Builds an <see cref="AsyncRetryPolicy{TResult}" /> that will retry <paramref name="retryCount" /> times
    ///     calling <paramref name="onRetryAsync" /> on each retry with the handled exception or result and retry count.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="policyBuilder">The policy builder.</param>
    /// <param name="retryCount">The retry count.</param>
    /// <param name="onRetryAsync">The action to call asynchronously on each retry.</param>
    /// <returns>The policy instance.</returns>
    /// <exception cref="ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Provide a non-null Action<DelegateResult<TResult>, int>, e.g. (outcome, i) => Log(outcome, i).
  2. Default optional callbacks in your wrapper to a no-op.
  3. Use the overload without onRetry if you need no per-retry side-effect.

Example fix

// before
Policy.HandleResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
    .RetryAsync(3, onRetry: null);
// after
Policy.HandleResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
    .RetryAsync(3, onRetry: (outcome, i) => Log.Retry(outcome, i));
Defensive patterns

Strategy: validation

Validate before calling

if (onRetry is null) throw new InvalidOperationException("onRetry callback required.");
// then call RetryAsync<TResult>(retryCount, onRetry)

Type guard

static bool IsValidOnRetry(Action<DelegateResult<TResult>, int> a) => a is not null;

Try / catch

try { policy = Policy.HandleResult<T>(IsHandled).RetryAsync(retryCount, onRetry); }
catch (ArgumentNullException ex) when (ex.ParamName == nameof(onRetry))
{ /* log config error, use no-op callback */ }

Prevention

When it happens

Trigger: Calling RetryAsync<TResult>(retryCount, onRetry: null) on a PolicyBuilder<TResult>, or passing an optional Action that defaulted to null.

Common situations: Building a result-typed retry (e.g. for handling HttpResponseMessage with bad status codes) and forgetting the logging hook, or wiring through a wrapper that forwards an unset parameter.

Related errors


AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13). Data as JSON: /api/errors/f4a85f3e332be266. Report an issue: GitHub.