-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolly - Retry.linq
52 lines (45 loc) · 886 Bytes
/
Polly - Retry.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<Query Kind="Program">
<NuGetReference>Polly</NuGetReference>
<Namespace>Polly</Namespace>
</Query>
void Main()
{
for (int i = 0; i < 10; i++)
{
try
{
Console.WriteLine("i = {0} X = {1}",
i,
FailFirstFiveTimes());
}
catch (Exception e)
{
Console.WriteLine("i = {0} exception = {1}", i, e.Message);
}
}
Reset();
Console.WriteLine("-------------------------------------------------------------");
// exception, count or index, context
Policy.Handle<Exception>().Retry(6, onRetry: (e, i, c) =>
{
Console.WriteLine("i = {0} e = {1}", i, e?.Message);
})
.Execute(() =>
{
FailFirstFiveTimes();
Console.WriteLine("success?");
});
}
int countFailFirstFiveTimes = 0;
bool FailFirstFiveTimes()
{
if (this.countFailFirstFiveTimes++ < 5)
{
throw new Exception("fail");
}
return true;
}
void Reset()
{
this.countFailFirstFiveTimes = 0;
}