{"record":{"id":"6e7c86b648a379e7","repo":"App-vNext/Polly","slug":"onretryasync-6e7c86","errorCode":null,"errorMessage":"onRetryAsync","messagePattern":"onRetryAsync","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Polly/Retry/AsyncRetryTResultSyntax.cs","lineNumber":92,"sourceCode":"#pragma warning restore 1998\n    }\n\n    /// <summary>\n    ///     Builds an <see cref=\"AsyncRetryPolicy{TResult}\" /> that will retry <paramref name=\"retryCount\" /> times\n    ///     calling <paramref name=\"onRetryAsync\" /> on each retry with the handled exception or result and retry count.\n    /// </summary>\n    /// <typeparam name=\"TResult\">The type of the result.</typeparam>\n    /// <param name=\"policyBuilder\">The policy builder.</param>\n    /// <param name=\"retryCount\">The retry count.</param>\n    /// <param name=\"onRetryAsync\">The action to call asynchronously on each retry.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\">retryCount;Value must be greater than or equal to zero.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onRetryAsync\"/> is <see langword=\"null\"/>.</exception>\n    public static AsyncRetryPolicy<TResult> RetryAsync<TResult>(this PolicyBuilder<TResult> policyBuilder, int retryCount, Func<DelegateResult<TResult>, int, Task> onRetryAsync)\n    {\n        if (onRetryAsync == null)\n        {\n            throw new ArgumentNullException(nameof(onRetryAsync));\n        }\n\n        return policyBuilder.RetryAsync(retryCount, onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i));\n    }\n\n    /// <summary>\n    /// Builds an <see cref=\"AsyncRetryPolicy{TResult}\"/> that will retry once\n    /// calling <paramref name=\"onRetry\"/> on retry with the handled exception or result, retry count and context data.\n    /// </summary>\n    /// <typeparam name=\"TResult\">The type of the result.</typeparam>\n    /// <param name=\"policyBuilder\">The policy builder.</param>\n    /// <param name=\"onRetry\">The action to call on each retry.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onRetry\"/> is <see langword=\"null\"/>.</exception>\n    public static AsyncRetryPolicy<TResult> RetryAsync<TResult>(this PolicyBuilder<TResult> policyBuilder, Action<DelegateResult<TResult>, int, Context> onRetry) =>\n        policyBuilder.RetryAsync(1, onRetry);\n\n    /// <summary>","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Retry/AsyncRetryTResultSyntax.cs#L74-L110","documentation":"Thrown by RetryAsync<TResult> when the async callback Func<DelegateResult<TResult>, int, Task> onRetryAsync is null. This overload forwards to the context-aware variant with a discarded context; it only null-checks onRetryAsync and delegates retryCount validation downstream. The callback is invoked and awaited on each retry, so null is rejected at construction.","triggerScenarios":"Calling RetryAsync<TResult>(retryCount, onRetryAsync: null), or accepting an optional Func from a wrapper and passing it through unguarded.","commonSituations":"Wrapper that exposes an optional async retry hook, or DI resolving the callback as null.","solutions":["Pass a non-null Func<DelegateResult<TResult>, int, Task>.","Coalesce the optional argument: onRetryAsync ?? NoOpRetryAsync.","Pick a parameterless-onRetry overload if no hook is required."],"exampleFix":"// before\n.RetryAsync(3, onRetryAsync: null);\n// after\n.RetryAsync(3, onRetryAsync: async (outcome, i) => await Log.RetryAsync(outcome, i));","handlingStrategy":"validation","validationCode":"if (onRetryAsync is null) throw new InvalidOperationException(\"onRetryAsync required.\");\n// then call RetryAsync<TResult>(retryCount, onRetryAsync)","typeGuard":"static bool IsValidOnRetry(Func<DelegateResult<TResult>, int, Task> f) => f is not null;","tryCatchPattern":"try { Policy.HandleResult<T>(IsHandled).RetryAsync(retryCount, onRetryAsync); }\ncatch (ArgumentNullException ex) when (ex.ParamName == nameof(onRetryAsync))\n{ /* supply a completed-task no-op callback */ }","preventionTips":["Default optional async hooks to a completed-task no-op.","Register async retry callbacks in DI as always-resolved services.","Turn on NRTs to catch null Func assignments at compile time."],"tags":["polly","retry","argument-null","callback","async","result-typed","legacy"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}