I have a query - Could you help me If have completed the number RetryCount
in WaitAndRetryAsync
but still not getting StatusCode.OK
. Then I want to through the exception. So How can I do this?
Or is there any way if a number or retry count is completed, so is there any way that can provide functionality OnRetryComplete
so that we can give any message that we have retried multiple times but not able to get the success code and can return any method.
var policy = Policy
.Handle<Exception>()
.OrResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
.WaitAndRetryAsync(2, _ => TimeSpan.FromMilliseconds(200));
try
{
int count = 0;
var responseMessage = await asyncPolicy.ExecuteAsync(async () =>
{
string requestJson = JsonSerializer.Serialize(eligRequest, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
using var content = new StringContent(requestJson, Encoding.UTF8, "application/json");
HttpResponseMessage result = await httpClient.PostAsync("test/api/ate", content);
return result.EnsureSuccessStatusCode();
});
return responseMessage;
}
catch (HttpRequestException error)
{
logger.LogError(error);
throw new CustomException();
}