Created
April 2, 2018 06:26
-
-
Save MarcosCobena/8cbd3e626a99d2c0f16fc752840ceb95 to your computer and use it in GitHub Desktop.
Demo scenario using Polly's Retry policy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private RetryPolicy CreateRetryPolicy() => Policy.Handle<Exception>() | |
| .WaitAndRetryAsync( | |
| Enumerable.Range(0, 3) | |
| .Select(_ => TimeSpan.FromSeconds(5)) | |
| .ToList()); | |
| [...] | |
| var policy = CreateRetryPolicy(); | |
| TResult result; | |
| try | |
| { | |
| result = await policy.ExecuteAsync(async () => | |
| { | |
| var response = await httpClient.PostAsync(uri, content); | |
| var jsonContent = await response.Content.ReadAsStringAsync(); | |
| var deserializedJson = await Task.Run( | |
| () => JsonConvert.DeserializeObject<TResult>(jsonContent, _serializerSettings)); | |
| return deserializedJson; | |
| }); | |
| } | |
| catch | |
| { | |
| result = default(TResult); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment