Skip to content

Commit f55880c

Browse files
authored
Fix Retry strategy example code (#2527)
* Fix ShouldHandle example * Fix retry-pattern-overusing-builder in snippet source * retry snippets: fix null-ref warning; IDE0305 suggestion
1 parent c5045c5 commit f55880c

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

docs/strategies/retry.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,18 @@ Use collections and simple predicate functions:
474474

475475
<!-- snippet: retry-pattern-overusing-builder -->
476476
```cs
477-
ImmutableArray<Type> networkExceptions = new[]
478-
{
477+
ImmutableArray<Type> networkExceptions =
478+
[
479479
typeof(SocketException),
480480
typeof(HttpRequestException),
481-
}.ToImmutableArray();
481+
];
482482

483-
ImmutableArray<Type> strategyExceptions = new[]
484-
{
483+
ImmutableArray<Type> strategyExceptions =
484+
[
485485
typeof(TimeoutRejectedException),
486486
typeof(BrokenCircuitException),
487487
typeof(RateLimitRejectedException),
488-
}.ToImmutableArray();
488+
];
489489

490490
ImmutableArray<Type> retryableExceptions = networkExceptions
491491
.Union(strategyExceptions)
@@ -494,7 +494,8 @@ ImmutableArray<Type> retryableExceptions = networkExceptions
494494
var retry = new ResiliencePipelineBuilder()
495495
.AddRetry(new()
496496
{
497-
ShouldHandle = ex => new ValueTask<bool>(retryableExceptions.Contains(ex.GetType())),
497+
ShouldHandle = args
498+
=> ValueTask.FromResult(args.Outcome.Exception != null && retryableExceptions.Contains(args.Outcome.Exception.GetType())),
498499
MaxRetryAttempts = 3,
499500
})
500501
.Build();

src/Snippets/Docs/Retry.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,18 @@ public static void Pattern_OverusingBuilder()
174174
{
175175
#region retry-pattern-overusing-builder
176176

177-
ImmutableArray<Type> networkExceptions = new[]
178-
{
177+
ImmutableArray<Type> networkExceptions =
178+
[
179179
typeof(SocketException),
180180
typeof(HttpRequestException),
181-
}.ToImmutableArray();
181+
];
182182

183-
ImmutableArray<Type> strategyExceptions = new[]
184-
{
183+
ImmutableArray<Type> strategyExceptions =
184+
[
185185
typeof(TimeoutRejectedException),
186186
typeof(BrokenCircuitException),
187187
typeof(RateLimitRejectedException),
188-
}.ToImmutableArray();
188+
];
189189

190190
ImmutableArray<Type> retryableExceptions = networkExceptions
191191
.Union(strategyExceptions)
@@ -194,7 +194,9 @@ public static void Pattern_OverusingBuilder()
194194
var retry = new ResiliencePipelineBuilder()
195195
.AddRetry(new()
196196
{
197-
ShouldHandle = ex => new ValueTask<bool>(retryableExceptions.Contains(ex.GetType())),
197+
ShouldHandle = args
198+
=> ValueTask.FromResult(args.Outcome.Exception is not null
199+
&& retryableExceptions.Contains(args.Outcome.Exception.GetType())),
198200
MaxRetryAttempts = 3,
199201
})
200202
.Build();

0 commit comments

Comments
 (0)