|
1 | 1 | using Polly;
|
2 | 2 | using Polly.Retry;
|
| 3 | +using Retries; |
3 | 4 | using System.Net;
|
4 | 5 |
|
| 6 | +var helper = new ExecuteHelper(); |
| 7 | + |
5 | 8 | // ------------------------------------------------------------------------
|
6 |
| -// 1. Create a retry strategy that only handles invalid operation exceptions |
| 9 | +// 1. Create a retry strategy that handles all exceptions |
7 | 10 | // ------------------------------------------------------------------------
|
8 | 11 |
|
9 | 12 | ResilienceStrategy strategy = new ResilienceStrategyBuilder()
|
10 |
| - .AddRetry(new RetryStrategyOptions |
11 |
| - { |
12 |
| - // Specify what exceptions should be retried |
13 |
| - ShouldRetry = outcome => |
14 |
| - { |
15 |
| - if (outcome.Exception is InvalidOperationException) |
16 |
| - { |
17 |
| - return PredicateResult.True; |
18 |
| - } |
19 |
| - |
20 |
| - return PredicateResult.False; |
21 |
| - }, |
22 |
| - }) |
| 13 | + // Default retry options handle all exceptions |
| 14 | + .AddRetry(new RetryStrategyOptions()) |
23 | 15 | .Build();
|
24 | 16 |
|
| 17 | +Console.WriteLine("---------------------------------------"); |
| 18 | +strategy.Execute(helper.ExecuteUnstable); |
| 19 | + |
25 | 20 | // ------------------------------------------------------------------------
|
26 | 21 | // 2. Customize the retry behavior
|
27 | 22 | // ------------------------------------------------------------------------
|
28 | 23 |
|
29 | 24 | strategy = new ResilienceStrategyBuilder()
|
30 | 25 | .AddRetry(new RetryStrategyOptions
|
31 | 26 | {
|
32 |
| - // Specify what exceptions should be retried |
33 |
| - ShouldRetry = outcome => |
34 |
| - { |
35 |
| - if (outcome.Exception is InvalidOperationException) |
36 |
| - { |
37 |
| - return PredicateResult.True; |
38 |
| - } |
39 |
| - |
40 |
| - return PredicateResult.False; |
41 |
| - }, |
| 27 | + // Specify what exceptions should be retried using PredicateBuilder |
| 28 | + ShouldHandle = new PredicateBuilder().Handle<InvalidOperationException>(), |
42 | 29 | RetryCount = 4,
|
43 | 30 | BaseDelay = TimeSpan.FromSeconds(1),
|
44 | 31 |
|
|
48 | 35 | })
|
49 | 36 | .Build();
|
50 | 37 |
|
| 38 | +Console.WriteLine("---------------------------------------"); |
| 39 | +strategy.Execute(helper.ExecuteUnstable); |
| 40 | + |
51 | 41 | // ------------------------------------------------------------------------
|
52 | 42 | // 3. Register the callbacks
|
53 | 43 | // ------------------------------------------------------------------------
|
54 | 44 |
|
55 | 45 | strategy = new ResilienceStrategyBuilder()
|
56 | 46 | .AddRetry(new RetryStrategyOptions
|
57 | 47 | {
|
58 |
| - // Specify what exceptions should be retried |
59 |
| - ShouldRetry = outcome => |
| 48 | + // Specify what exceptions should be retried using switch expressions |
| 49 | + ShouldHandle = args => args.Exception switch |
60 | 50 | {
|
61 |
| - if (outcome.Exception is InvalidOperationException) |
62 |
| - { |
63 |
| - return PredicateResult.True; |
64 |
| - } |
65 |
| - |
66 |
| - return PredicateResult.False; |
| 51 | + InvalidOperationException => PredicateResult.True, |
| 52 | + _ => PredicateResult.False, |
67 | 53 | },
|
68 |
| - |
69 | 54 | OnRetry = outcome =>
|
70 | 55 | {
|
71 | 56 | Console.WriteLine($"Retrying attempt {outcome.Arguments.Attempt}...");
|
|
74 | 59 | })
|
75 | 60 | .Build();
|
76 | 61 |
|
| 62 | +Console.WriteLine("---------------------------------------"); |
| 63 | +strategy.Execute(helper.ExecuteUnstable); |
| 64 | + |
77 | 65 | // ------------------------------------------------------------------------
|
78 | 66 | // 4. Create an HTTP retry strategy that handles both exceptions and results
|
79 | 67 | // ------------------------------------------------------------------------
|
|
82 | 70 | .AddRetry(new RetryStrategyOptions<HttpResponseMessage>
|
83 | 71 | {
|
84 | 72 | // Specify what exceptions or results should be retried
|
85 |
| - ShouldRetry = outcome => |
86 |
| - { |
87 |
| - // Now, also handle results |
88 |
| - if (outcome.Result?.StatusCode == HttpStatusCode.InternalServerError) |
89 |
| - { |
90 |
| - return PredicateResult.True; |
91 |
| - } |
92 |
| - |
93 |
| - if (outcome.Exception is InvalidOperationException) |
94 |
| - { |
95 |
| - return PredicateResult.True; |
96 |
| - } |
97 |
| - |
98 |
| - return PredicateResult.False; |
99 |
| - }, |
100 |
| - |
| 73 | + ShouldHandle = new PredicateBuilder<HttpResponseMessage>() |
| 74 | + .Handle<HttpRequestException>() |
| 75 | + .Handle<InvalidOperationException>() |
| 76 | + .HandleResult(r=>r.StatusCode == HttpStatusCode.InternalServerError), |
101 | 77 | // Specify delay generator
|
102 | 78 | RetryDelayGenerator = outcome =>
|
103 | 79 | {
|
|
112 | 88 | }
|
113 | 89 | })
|
114 | 90 | .Build();
|
| 91 | + |
| 92 | +Console.WriteLine("---------------------------------------"); |
| 93 | +httpStrategy.Execute(helper.ExecuteUnstable); |
0 commit comments