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

  1. Provide a non-null Action<Context,string> for onCachePut (a no-op is fine).
  2. Switch to a simpler overload that defaults to EmptyCallback when you do not need put notifications.
  3. 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

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


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