App-vNext/Polly · error · ArgumentNullException
Value cannot be null. (Parameter 'onCachePut')
Error message
Value cannot be null. (Parameter 'onCachePut')
What it means
Thrown by the terminal CacheAsync<TResult>(...) overload when onCachePut is null. The put callback is the last mandatory lifecycle hook in this overload; a null would NPE when storing a fresh value, so construction is rejected. All preceding guards run first.
Source
Thrown at src/Polly/Caching/AsyncCacheTResultSyntax.cs:1006
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 AsyncCachePolicy<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
}
private static void EmptyCallback(Context context, string key)
{
// No-op
}
}
View on GitHub (pinned to d0e46bdb1e)
Solutions
- Provide a non-null Action<Context,string> for onCachePut (a no-op is fine).
- Switch to a simpler overload that defaults to EmptyCallback when you do not need put notifications.
- Null-check at the call site.
Example fix
// before
var policy = Policy.CacheAsync<string>(provider, ttlStrategy, keyFunc, onGet, onMiss, null, null, null);
// after
Action<Context, string> noop = (_, _) => { };
var policy = Policy.CacheAsync<string>(provider, ttlStrategy, keyFunc, onGet, onMiss, noop, null, null); Defensive patterns
Strategy: validation
Validate before calling
Action<Context, string> onPut = onCachePut ?? ((_, _) => { });
var policy = Policy.CacheAsync<TResult>(provider, ttlStrategy, keyFunc, onGet, onMiss, onPut, null, null); Type guard
static bool HasCallback(Action<Context, string>? a) => a is not null;
Try / catch
try { var p = Policy.CacheAsync<R>(provider, ttl, keyFunc, onGet, onMiss, onPut, null, null); }
catch (ArgumentNullException ex) when (ex.ParamName == "onCachePut") { /* supply a no-op callback */ } Prevention
- Coalesce the put callback to a no-op.
- Prefer simpler overloads that default callbacks.
- Enable nullable reference types.
When it happens
Trigger: Calling the full overload with null for the onCachePut Action<Context,string>.
Common situations: Believing the callback is optional; refactor dropped the assignment; passing null placeholders from a builder that did not wire all callbacks.
Related errors
- Value cannot be null. (Parameter 'onCacheGet')
- Value cannot be null. (Parameter 'onCacheMiss')
- Value cannot be null. (Parameter 'ttlStrategy')
- Value cannot be null. (Parameter 'wrappedCacheProvider')
- Value cannot be null. (Parameter 'action')
AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13).
Data as JSON: /api/errors/fd280ad5e4935784.
Report an issue: GitHub.