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
- Pass a non-null Action<Context,string> for onCachePut (e.g. an empty lambda (_) => { }).
- If you do not need lifecycle telemetry, use a simpler overload.
- 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
- Provide a no-op action for callbacks you do not need.
- Prefer the simpler overload without explicit callbacks.
- Centralize callback wiring to avoid leaving one null.
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
- Value cannot be null. (Parameter 'onCacheGet')
- Value cannot be null. (Parameter 'onCacheMiss')
- Value cannot be null. (Parameter 'cacheKeyStrategy')
- Value cannot be null. (Parameter 'cacheProvider')
- Value cannot be null. (Parameter 'ttlStrategy')
AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13).
Data as JSON: /api/errors/ae73eb8251e29c3c.
Report an issue: GitHub.