{"record":{"id":"b171c9912ddf3c47","repo":"App-vNext/Polly","slug":"value-cannot-be-null-parameter-cacheprovider-b171c9","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/CacheTResultSyntax.cs","lineNumber":22,"sourceCode":"public partial class Policy\n{\n    /// <summary>\n    /// <para>Builds a <see cref=\"Policy{TResult}\"/> that will function like a result cache for delegate executions returning a <typeparamref name=\"TResult\"/>.</para>\n    /// <para>Before executing a delegate, checks whether the <paramref name=\"cacheProvider\"/> holds a value for the cache key specified by <see cref=\"Context.OperationKey\"/>.\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    /// <typeparam name=\"TResult\">The type of the result.</typeparam>\n    /// <param name=\"cacheProvider\">The cache provider.</param>\n    /// <param name=\"ttl\">Duration (ttl) for which to cache values.</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    public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string, Exception>? onCacheError = null)\n    {\n        if (cacheProvider == null)\n        {\n            throw new ArgumentNullException(nameof(cacheProvider));\n        }\n\n        return Cache(cacheProvider.For<TResult>(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError);\n    }\n\n    /// <summary>\n    /// <para>Builds a <see cref=\"Policy{TResult}\"/> that will function like a result cache for delegate executions returning a <typeparamref name=\"TResult\"/>.</para>\n    /// <para>Before executing a delegate, checks whether the <paramref name=\"cacheProvider\"/> holds a value for the cache key specified by <see cref=\"Context.OperationKey\"/>.\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    /// <typeparam name=\"TResult\">The type of the result.</typeparam>\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=\"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>","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Caching/CacheTResultSyntax.cs#L4-L40","documentation":"Thrown at policy-construction time by the legacy Polly v7 generic overload CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider, TimeSpan ttl, ...). The provider is the core dependency, so a null value is rejected immediately with ArgumentNullException. The overload then calls cacheProvider.For<TResult>() to obtain a typed provider.","triggerScenarios":"Calling Policy.Cache<TResult>(null, ttl) (or passing a null ISyncCacheProvider) on the typed-result overload.","commonSituations":"DI did not resolve the ISyncCacheProvider; the backing cache package is missing; you reused a provider variable that was set to null in a refactor.","solutions":["Pass a non-null ISyncCacheProvider that supports For<TResult>().","Ensure the caching provider package is installed and the provider is constructed/resolved before the call.","If the provider cannot be typed, build one that implements For<TResult> correctly."],"exampleFix":"// before\nPolicy.Cache<MyResult>(null, TimeSpan.FromMinutes(5));\n// after\nPolicy.Cache<MyResult>(memoryCacheProvider, TimeSpan.FromMinutes(5));","handlingStrategy":"validation","validationCode":"if (cacheProvider is null) throw new InvalidOperationException(\"cacheProvider is not configured.\");\nvar policy = Policy.Cache<TResult>(cacheProvider, ttl);","typeGuard":"static bool IsCacheProviderValid(Polly.Caching.ISyncCacheProvider? p) => p is not null;","tryCatchPattern":null,"preventionTips":["Ensure the provider supports For<TResult>() (most standard providers do).","Resolve ISyncCacheProvider from DI so missing registrations fail at startup.","Assert the provider is non-null in a startup check."],"tags":["polly","caching","legacy-v7","argument-null","cache-provider","generic"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}