App-vNext/Polly · error · ArgumentNullException

Value cannot be null. (Parameter 'cacheProvider')

Error message

Value cannot be null. (Parameter 'cacheProvider')

What it means

Thrown by CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context,string> cacheKeyStrategy, ...) (AsyncCacheTResultSyntax.cs:157) when cacheProvider is null. This is the terminal typed-result Func-key convenience overload; it validates only cacheProvider here and forwards to the 8-parameter overload for the rest.

Source

Thrown at src/Polly/Caching/AsyncCacheTResultSyntax.cs:161

    /// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
    /// <para>Before executing a delegate, checks whether the <paramref name="cacheProvider" /> holds a value for the cache key determined by applying the <paramref name="cacheKeyStrategy"/> to the execution <see cref="Context"/>.
    /// If the <paramref name="cacheProvider" /> provides a value from cache, returns that value and does not execute the governed delegate.  If the <paramref name="cacheProvider" /> does not provide a value, executes the governed delegate, stores the value with the <paramref name="cacheProvider" />, then returns the value.
    /// </para>
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="cacheProvider">The cache provider.</param>
    /// <param name="ttlStrategy">A strategy for specifying ttl for values to be cached.</param>
    /// <param name="cacheKeyStrategy">The cache key strategy.</param>
    /// <param name="onCacheError">Delegate to call if an exception is thrown when attempting to get a value from or put a value into the cache, passing the execution context, the cache key, and the exception.</param>
    /// <returns>The policy instance.</returns>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
    public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
    {
        if (cacheProvider == null)
        {
            throw new ArgumentNullException(nameof(cacheProvider));
        }

        return CacheAsync(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy, onCacheError);
    }

    /// <summary>
    /// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
    /// <para>Before executing a delegate, checks whether the <paramref name="cacheProvider" /> holds a value for the cache key.
    /// If the <paramref name="cacheProvider" /> provides a value from cache, returns that value and does not execute the governed delegate.  If the <paramref name="cacheProvider" /> does not provide a value, executes the governed delegate, stores the value with the <paramref name="cacheProvider" />, then returns the value.
    /// </para>
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="cacheProvider">The cache provider.</param>
    /// <param name="ttl">Duration (ttl) for which to cache values.</param>
    /// <param name="onCacheGet">Delegate to call on a cache hit, when value is returned from cache.</param>
    /// <param name="onCacheMiss">Delegate to call on a cache miss.</param>
    /// <param name="onCachePut">Delegate to call on cache put.</param>
    /// <param name="onCacheGetError">Delegate to call if an exception is thrown when attempting to get a value from the cache, passing the execution context, the cache key, and the exception.</param>

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Pass a non-null IAsyncCacheProvider.
  2. Register the cache provider as a DI singleton and assert non-null at startup.

Example fix

// before
Policy.CacheAsync<MyResult>(null, ttlStrategy, ctx => ctx.OperationKey);

// after
Policy.CacheAsync<MyResult>(cacheProvider, ttlStrategy, ctx => ctx.OperationKey);
Defensive patterns

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(cacheProvider);
var policy = Policy.CacheAsync<TResult>(cacheProvider, ttlStrategy, keyFunc, onCacheError);

Type guard

static bool IsCacheProviderValid(IAsyncCacheProvider? p) => p is not null;

Prevention

When it happens

Trigger: Calling Policy.CacheAsync<TResult>(null, ttlStrategy, keyFunc, onCacheError).

Common situations: Provider injected as null; migration to distributed cache that failed to construct.

Related errors


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