diff --git a/src/Polly.Core/ResilienceProperties.cs b/src/Polly.Core/ResilienceProperties.cs index 466901436a..6866be4a19 100644 --- a/src/Polly.Core/ResilienceProperties.cs +++ b/src/Polly.Core/ResilienceProperties.cs @@ -20,10 +20,18 @@ public sealed class ResilienceProperties /// True, if a property was retrieved. public bool TryGetValue(ResiliencePropertyKey key, [MaybeNullWhen(false)] out TValue value) { - if (Options.TryGetValue(key.Key, out object? val) && val is TValue typedValue) + if (Options.TryGetValue(key.Key, out object? val)) { - value = typedValue; - return true; + if (val is TValue typedValue) + { + value = typedValue; + return true; + } + else if (val is null) + { + value = default!; + return true; + } } value = default; diff --git a/test/Polly.Core.Tests/ResiliencePropertiesTests.cs b/test/Polly.Core.Tests/ResiliencePropertiesTests.cs index e4ef2c31ba..45e97016b7 100644 --- a/test/Polly.Core.Tests/ResiliencePropertiesTests.cs +++ b/test/Polly.Core.Tests/ResiliencePropertiesTests.cs @@ -14,6 +14,18 @@ public void TryGetValue_Ok() val.Should().Be(12345); } + [Fact] + public void TryGetValue_ValueIsNull_Ok() + { + var key = new ResiliencePropertyKey("dummy"); + var props = new ResilienceProperties(); + + props.Set(key, null); + + props.TryGetValue(key, out var val).Should().Be(true); + val.Should().Be(null); + } + [Fact] public void TryGetValue_NotFound_Ok() { @@ -34,6 +46,17 @@ public void GetValue_Ok() props.GetValue(key, default).Should().Be(12345); } + [Fact] + public void GetValue_ValueIsNull_Ok() + { + var key = new ResiliencePropertyKey("dummy"); + var props = new ResilienceProperties(); + + props.Set(key, null); + + props.GetValue(key, "default").Should().Be(null); + } + [Fact] public void GetValue_NotFound_EnsureDefault() {