App-vNext/Polly · error · ArgumentNullException

Value cannot be null. (Parameter 'onCacheMiss')

Error message

Value cannot be null. (Parameter 'onCacheMiss')

What it means

Thrown at policy-construction time by the legacy Polly v7 base overload Policy.Cache(...) with full callbacks. onCacheMiss is invoked when no cached value is found; this detailed overload requires it to be non-null because the policy calls it on every cache miss. Polly throws ArgumentNullException to fail fast.

Source

Thrown at src/Polly/Caching/CacheSyntax.cs:363

        if (ttlStrategy == null)
        {
            throw new ArgumentNullException(nameof(ttlStrategy));
        }

        if (cacheKeyStrategy == null)
        {
            throw new ArgumentNullException(nameof(cacheKeyStrategy));
        }

        if (onCacheGet == null)
        {
            throw new ArgumentNullException(nameof(onCacheGet));
        }

        if (onCacheMiss == null)
        {
            throw new ArgumentNullException(nameof(onCacheMiss));
        }

        if (onCachePut == null)
        {
            throw new ArgumentNullException(nameof(onCachePut));
        }

        return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
    }
}

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Pass a non-null Action<Context,string> for onCacheMiss (e.g. an empty lambda (_) => { }).
  2. If you do not need lifecycle telemetry, use a simpler overload.
  3. Provide a shared static no-op action for callbacks you do not care about.

Example fix

// before
Policy.Cache(provider, ttlStrategy, keyFunc, onGet, null, onPut, onGetErr, onPutErr);
// after
Policy.Cache(provider, ttlStrategy, keyFunc, onGet, (ctx, key) => { /* log miss */ }, onPut, onGetErr, onPutErr);
Defensive patterns

Strategy: validation

Validate before calling

if (onCacheMiss is null) throw new InvalidOperationException("onCacheMiss is required by this overload.");
var policy = Policy.Cache(cacheProvider, ttlStrategy, keyFunc, onGet, onCacheMiss, onPut, onGetErr, onPutErr);

Type guard

static bool IsCallbackValid(System.Action<Polly.Context,string>? a) => a is not null;

Prevention

When it happens

Trigger: Calling the full-callback Cache(...) overload with the onCacheMiss argument set to null.

Common situations: You supplied onCacheGet and onCachePut for telemetry but omitted onCacheMiss; you assumed all callbacks were optional in this overload (only the *Error callbacks are nullable); refactor left it null.

Related errors


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