diff --git a/src/Polly/Caching/DefaultCacheKeyStrategy.cs b/src/Polly/Caching/DefaultCacheKeyStrategy.cs
index 12272f73eff..5cff753f529 100644
--- a/src/Polly/Caching/DefaultCacheKeyStrategy.cs
+++ b/src/Polly/Caching/DefaultCacheKeyStrategy.cs
@@ -4,7 +4,6 @@ namespace Polly.Caching;
///
/// The default cache key strategy for . Returns the property .
///
-#pragma warning disable CA1062 // Validate arguments of public methods
public class DefaultCacheKeyStrategy : ICacheKeyStrategy
{
///
@@ -12,8 +11,15 @@ public class DefaultCacheKeyStrategy : ICacheKeyStrategy
///
/// The execution context.
/// The cache key.
- public string GetCacheKey(Context context) =>
- context.OperationKey;
+ public string GetCacheKey(Context context)
+ {
+ if (context is null)
+ {
+ throw new ArgumentNullException(nameof(context));
+ }
+
+ return context.OperationKey;
+ }
///
/// Gets an instance of the .
diff --git a/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs b/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs
index c81162040d4..0158dc06a22 100644
--- a/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs
+++ b/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs
@@ -2,6 +2,14 @@
public class DefaultCacheKeyStrategySpecs
{
+ [Fact]
+ public void Should_throw_when_context_is_null()
+ {
+ Context context = null!;
+ Action action = () => DefaultCacheKeyStrategy.Instance.GetCacheKey(context);
+ action.Should().Throw().And.ParamName.Should().Be("context");
+ }
+
[Fact]
public void Should_return_Context_OperationKey_as_cache_key()
{