{"record":{"id":"3d3f72a4097e282a","repo":"App-vNext/Polly","slug":"value-cannot-be-null-parameter-cacheprovider-3d3f72","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'cacheProvider')","messagePattern":"Value cannot be null\\. \\(Parameter 'cacheProvider'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Polly/Caching/CacheSyntax.cs","lineNumber":80,"sourceCode":"    /// <summary>\n    /// <para>Builds a <see cref=\"Policy\"/> that will function like a result cache for delegate executions returning a result.</para>\n    /// <para>Before executing a delegate returning a result, 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\"/>.\n    /// 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.\n    /// </para>\n    /// </summary>\n    /// <param name=\"cacheProvider\">The cache provider.</param>\n    /// <param name=\"ttlStrategy\">A strategy for specifying ttl for values to be cached.</param>\n    /// <param name=\"cacheKeyStrategy\">The cache key strategy.</param>\n    /// <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>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"cacheProvider\"/> is <see langword=\"null\"/>.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"ttlStrategy\"/> is <see langword=\"null\"/>.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"cacheKeyStrategy\"/> is <see langword=\"null\"/>.</exception>\n    public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)\n    {\n        if (cacheProvider == null)\n        {\n            throw new ArgumentNullException(nameof(cacheProvider));\n        }\n\n        if (ttlStrategy == null)\n        {\n            throw new ArgumentNullException(nameof(ttlStrategy));\n        }\n\n        if (cacheKeyStrategy == null)\n        {\n            throw new ArgumentNullException(nameof(cacheKeyStrategy));\n        }\n\n        return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, EmptyCallback, EmptyCallback, EmptyCallback, onCacheError, onCacheError);\n    }\n\n    /// <summary>\n    /// <para>Builds a <see cref=\"Policy\"/> that will function like a result cache for delegate executions returning a result.</para>\n    /// <para>Before executing a delegate returning a result, 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\"/>.","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Caching/CacheSyntax.cs#L62-L98","documentation":"Thrown at policy-construction time by the legacy Polly v7 overload Policy.Cache(ISyncCacheProvider, ITtlStrategy, ICacheKeyStrategy, ...). The cache provider is the core dependency of a CachePolicy (it actually stores/retrieves cached values), so Polly rejects a null provider immediately with ArgumentNullException. Without it the policy cannot function.","triggerScenarios":"Calling Policy.Cache(null, ttlStrategy, cacheKeyStrategy, ...) on the ITtlStrategy-based overload, i.e. the ISyncCacheProvider argument is null.","commonSituations":"The cache provider was never instantiated (e.g. you forgot to wrap a MemoryCache with CacheProvider.Memory/Microsoft.Extensions.Caching.Memory); a DI-injected ISyncCacheProvider resolved to null because the package providing a provider was not referenced; refactoring changed the provider type but not the call site.","solutions":["Pass a non-null ISyncCacheProvider (e.g. a registered MemoryCacheProvider, or the provider from CacheProvider.Memory(...)).","Verify the caching provider package is installed and the provider is resolved from DI before calling Cache(...).","Use the simpler overload that takes a TimeSpan ttl if you do not have an ITtlStrategy."],"exampleFix":"// before\nPolicy.Cache(null, new RelativeTtl(ttl), myStrategy);\n// after\nPolicy.Cache(memoryCacheProvider, new RelativeTtl(ttl), myStrategy);","handlingStrategy":"validation","validationCode":"if (cacheProvider is null) throw new InvalidOperationException(\"cacheProvider is not registered.\");\nvar policy = Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy);","typeGuard":"static bool IsCacheProviderValid(Polly.Caching.ISyncCacheProvider? p) => p is not null;","tryCatchPattern":null,"preventionTips":["Register the cache provider package (e.g. Polly.Caching.MemoryCache) and resolve ISyncCacheProvider from DI.","Assert the provider is non-null in a startup health check.","Keep a single composition root that builds the CachePolicy so wiring is verified once."],"tags":["polly","caching","legacy-v7","argument-null","cache-provider"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}