App-vNext/Polly · error · ArgumentNullException

Value cannot be null. (Parameter 'onCachePut')

Error message

Value cannot be null. (Parameter 'onCachePut')

What it means

Thrown at policy-construction time by the legacy Polly v7 base overload Policy.Cache(...) with full callbacks. onCachePut is invoked after a fresh value is written to cache; this detailed overload requires it non-null and invokes it on every cache store. Polly validates it up front with ArgumentNullException.

Source

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

        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 onCachePut (e.g. an empty lambda (_) => { }).
  2. If you do not need lifecycle telemetry, use a simpler overload.
  3. Use a shared static no-op action for callbacks you do not use.

Example fix

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

Strategy: validation

Validate before calling

if (onCachePut is null) throw new InvalidOperationException("onCachePut is required by this overload.");
var policy = Policy.Cache(cacheProvider, ttlStrategy, keyFunc, onGet, onMiss, onCachePut, 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 onCachePut argument set to null.

Common situations: You supplied onCacheGet/onCacheMiss but omitted onCachePut; you assumed callbacks were optional in this overload; refactor left it null.

Related errors


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