Skip to content

Instantly share code, notes, and snippets.

@MarcosCobena
Created April 2, 2018 06:26
Show Gist options
  • Select an option

  • Save MarcosCobena/8cbd3e626a99d2c0f16fc752840ceb95 to your computer and use it in GitHub Desktop.

Select an option

Save MarcosCobena/8cbd3e626a99d2c0f16fc752840ceb95 to your computer and use it in GitHub Desktop.
Demo scenario using Polly's Retry policy
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