diff --git a/src/Polly/Caching/ContextualTtl.cs b/src/Polly/Caching/ContextualTtl.cs
index 3368f0464c..1a06c59ec9 100644
--- a/src/Polly/Caching/ContextualTtl.cs
+++ b/src/Polly/Caching/ContextualTtl.cs
@@ -4,7 +4,6 @@ namespace Polly.Caching;
///
/// Defines a ttl strategy which will cache items for a TimeSpan which may be influenced by data in the execution context.
///
-#pragma warning disable CA1062 // Validate arguments of public methods
public class ContextualTtl : ITtlStrategy
{
///
@@ -28,6 +27,11 @@ public class ContextualTtl : ITtlStrategy
/// TimeSpan.
public Ttl GetTtl(Context context, object? result)
{
+ if (context is null)
+ {
+ throw new ArgumentNullException(nameof(context));
+ }
+
if (!context.ContainsKey(TimeSpanKey))
{
return NoTtl;
diff --git a/test/Polly.Specs/Caching/ContextualTtlSpecs.cs b/test/Polly.Specs/Caching/ContextualTtlSpecs.cs
index 411d79fbae..0cd272bb19 100644
--- a/test/Polly.Specs/Caching/ContextualTtlSpecs.cs
+++ b/test/Polly.Specs/Caching/ContextualTtlSpecs.cs
@@ -2,6 +2,14 @@
public class ContextualTtlSpecs
{
+ [Fact]
+ public void Should_throw_when_context_is_null()
+ {
+ Context context = null!;
+ Action action = () => new ContextualTtl().GetTtl(context, null);
+ action.Should().Throw().And.ParamName.Should().Be("context");
+ }
+
[Fact]
public void Should_return_zero_if_no_value_set_on_context() =>
new ContextualTtl().GetTtl(new Context("someOperationKey"), null).Timespan.Should().Be(TimeSpan.Zero);