-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
39 lines (37 loc) · 1.52 KB
/
Program.cs
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
using System;
using System.Threading.Tasks;
using System.Net.Http;
using Polly;
namespace PollyTest
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var httpClient = new HttpClient();
var response = await Policy
.HandleResult<HttpResponseMessage>(message=>!message.IsSuccessStatusCode)
//.WaitAndRetryAsync(3,i=>TimeSpan.FromSeconds(5),(result,timeSpan,retryCount,context)=>
//{
// Console.WriteLine("Request failed with {0} Waiting {1} before next retry. Retry attempt {2}", result.Result.StatusCode,timeSpan,retryCount);
//})
//exponential backoff
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(3),
TimeSpan.FromSeconds(5)
},(result,timeSpan,retryCount,context)=>
{
Console.WriteLine("Request failed with {0} Waiting {1} before next retry. Retry attempt {2}", result.Result.StatusCode, timeSpan, retryCount);
})
.ExecuteAsync(()=>httpClient.GetAsync("http://www.dinamalar.com"));
if (response.IsSuccessStatusCode)
Console.WriteLine("Response was successful");
else
Console.WriteLine("Response failed. Status code {0}",response.StatusCode);
Console.ReadKey();
}
}
}