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
- Pass a non-null Action to policy.Execute.
- If the delegate is optional, branch before calling Execute rather than passing null.
- 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
- Branch around policy.Execute when the delegate is optional.
- Null-check delegates at the call site.
- Avoid adapter layers that forward null delegates.
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
- Value cannot be null. (Parameter 'ttlStrategy')
- Value cannot be null. (Parameter 'onCacheGet')
- Value cannot be null. (Parameter 'onCacheMiss')
- Value cannot be null. (Parameter 'onCachePut')
- Value cannot be null. (Parameter 'wrappedCacheProvider')
AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13).
Data as JSON: /api/errors/d14489de0cb8fd5e.
Report an issue: GitHub.