{"record":{"id":"588f6767f5ffd59f","repo":"App-vNext/Polly","slug":"value-cannot-be-null-parameter-cachekeystrategy","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'cacheKeyStrategy')","messagePattern":"Value cannot be null\\. \\(Parameter 'cacheKeyStrategy'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Polly/Caching/AsyncCacheTResultSyntax.cs","lineNumber":278,"sourceCode":"    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onCachePut\"/> is <see langword=\"null\"/>.</exception>\n    public static AsyncCachePolicy<TResult> CacheAsync<TResult>(\n        IAsyncCacheProvider cacheProvider,\n        TimeSpan ttl,\n        ICacheKeyStrategy cacheKeyStrategy,\n        Action<Context, string> onCacheGet,\n        Action<Context, string> onCacheMiss,\n        Action<Context, string> onCachePut,\n        Action<Context, string, Exception>? onCacheGetError,\n        Action<Context, string, Exception>? onCachePutError)\n    {\n        if (cacheProvider == null)\n        {\n            throw new ArgumentNullException(nameof(cacheProvider));\n        }\n\n        if (cacheKeyStrategy is null)\n        {\n            throw new ArgumentNullException(nameof(cacheKeyStrategy));\n        }\n\n        return CacheAsync(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);\n    }\n\n    /// <summary>\n    /// <para>Builds an <see cref=\"AsyncPolicy{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 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    /// <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=\"cacheKeyStrategy\">The cache key strategy.</param>\n    /// <param name=\"onCacheGet\">Delegate to call on a cache hit, when value is returned from cache.</param>\n    /// <param name=\"onCacheMiss\">Delegate to call on a cache miss.</param>\n    /// <param name=\"onCachePut\">Delegate to call on cache put.</param>","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Caching/AsyncCacheTResultSyntax.cs#L260-L296","documentation":"Thrown by the 8-parameter typed-result overload CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action onCacheGet, ...) (AsyncCacheTResultSyntax.cs:261) when cacheKeyStrategy is null. The strategy is dereferenced for cacheKeyStrategy.GetCacheKey, so a null instance NREs and is therefore guarded.","triggerScenarios":"Calling Policy.CacheAsync<TResult>(cacheProvider, TimeSpan.FromMinutes(5), (ICacheKeyStrategy)null, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError).","commonSituations":"Switching to a custom key strategy but not instantiating it; the strategy DI registration missing.","solutions":["Pass a non-null ICacheKeyStrategy instance.","Use DefaultCacheKeyStrategy via an overload that omits the strategy argument if OperationKey suffices."],"exampleFix":"// before\nPolicy.CacheAsync<MyResult>(provider, TimeSpan.FromMinutes(5), (ICacheKeyStrategy)null, onGet, onMiss, onPut, onGetErr, onPutErr);\n\n// after\nPolicy.CacheAsync<MyResult>(provider, TimeSpan.FromMinutes(5), new MyKeyStrategy(), onGet, onMiss, onPut, onGetErr, onPutErr);","handlingStrategy":"validation","validationCode":"if (cacheKeyStrategy is null) throw new InvalidOperationException(\"cacheKeyStrategy must be provided.\");\nvar policy = Policy.CacheAsync<TResult>(cacheProvider, TimeSpan.FromMinutes(5), cacheKeyStrategy, onGet, onMiss, onPut, onGetErr, onPutErr);","typeGuard":"static bool IsKeyStrategyValid(ICacheKeyStrategy? s) => s is not null;","tryCatchPattern":null,"preventionTips":["Drop the cacheKeyStrategy argument and use the default-key overload when OperationKey suffices.","Construct the strategy inline at the call site.","Enable nullable reference types."],"tags":["polly","caching","argumentnullexception","cache-key","typed-result","csharp"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}