App-vNext/Polly · error · ArgumentNullException

Value cannot be null. (Parameter 'action')

Error message

Value cannot be null. (Parameter 'action')

What it means

Thrown by CachePolicy.Implementation(Action<Context,CancellationToken>, ...) when the action is null. This override handles void-returning delegate executions through a cache policy as a pass-through/NOOP (caching only applies to results), but it still requires a non-null action to execute. The guard prevents a NullReferenceException when invoking the delegate.

Source

Thrown at src/Polly/Caching/CachePolicy.cs:46

    {
        _syncCacheProvider = syncCacheProvider;
        _ttlStrategy = ttlStrategy;
        _cacheKeyStrategy = cacheKeyStrategy;

        _onCacheGet = onCacheGet;
        _onCachePut = onCachePut;
        _onCacheMiss = onCacheMiss;
        _onCacheGetError = onCacheGetError;
        _onCachePutError = onCachePutError;
    }

    /// <inheritdoc/>
    protected override void Implementation(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken)
    {
        // Pass-through/NOOP policy action, for void-returning calls through a cache policy.
        if (action is null)
        {
            throw new ArgumentNullException(nameof(action));
        }

        action(context, cancellationToken);
    }

    /// <inheritdoc/>
    [DebuggerStepThrough]
    protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
    {
        if (action is null)
        {
            throw new ArgumentNullException(nameof(action));
        }

        return CacheEngine.Implementation(
            _syncCacheProvider.For<TResult>(),
            _ttlStrategy.For<TResult>(),
            _cacheKeyStrategy,

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Pass a non-null Action to policy.Execute.
  2. If the delegate is optional, branch before calling Execute rather than passing null.
  3. Null-check the action before invoking the policy.

Example fix

// before
policy.Execute((Action<Context, CancellationToken>)null, context, ct);
// after
policy.Execute((ctx, token) => DoWork(ctx, token), context, ct);
Defensive patterns

Strategy: validation

Validate before calling

if (action is null) throw new InvalidOperationException("Cannot execute a null action through the cache policy.");
policy.Execute(action, context, cancellationToken);

Type guard

static bool HasAction(Action<Context, CancellationToken>? a) => a is not null;

Try / catch

try { policy.Execute(action, context, ct); }
catch (ArgumentNullException ex) when (ex.ParamName == "action") { /* supply a real action or skip execution */ }

Prevention

When it happens

Trigger: Executing a void-returning delegate through a CachePolicy and passing null for the action, e.g. policy.Execute((Action<Context,CancellationToken>)null, context, ct). More commonly reached when a wrapper/adapter forwards a null delegate.

Common situations: A code path conditionally supplies a delegate and the null branch was not handled; an adapter layer passes through a null action; tests invoke the policy with a null action by mistake.

Related errors


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