Introduction
Polly is a sophisticated .NET library that offers resiliency and fault-handling capabilities.
This library incorporates IHttpClientFactory and offers good transient-fault management and resiliency via policies such.
- Retry
- Circuit Breaker
- Timeout
- Bulkhead Isolation
- Fallback
If you need to add a resilient request, the client request can retry by sending the request several times.
Such retries typically address issues in the case of transitory errors, yielding the desired outcome for the request. Other regulations, such as circuit breakers or timeouts, can be implemented.
Install Polly NuGet package in our application.
Microsoft.Extensions.Http.Polly
Next, please add the following policy for HTTP retries.
// Create the retry policy we want
var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError()
.RetryAsync(3);
builder.Services.AddHttpClient("emp",(serviceProvider, client) =>
{
client.BaseAddress = new Uri("https://localhost:7061");
})
.AddPolicyHandler(retryPolicy);
In the above example, the policy is configured to try three times to retry the request, and after that, it will not try to execute the API request.
If you want to add an exponential retry policy, then you need to check the configuration below.
// Create the retry policy we want
var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError()
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
In the preceding example, the policy is set to try three times with an exponential retry interval beginning at two seconds.
Polly Error Handling in .NET
HandleTransientHttpError() automatically handles the HTTPS problems listed below.
- Network failures (such as System.Net.Http.HttpRequestException)
- HTTP 5XX status codes (server error)
- 500 (Internal Server Error),
- 502 (Bad Gateway),
- 503 (Service Unavailable), and
- 504 (Gateway Timeout).
- HTTP 408 Status Code (Request Timeout)
How to check that HTTP status code in the polly retry policy?
Apart from the above-mentioned transitory error, if you need to consider additional HTTP statuses for the retry process, please use the approach described below.
An updated Retry policy that takes into account additional HTTP Status codes can be added by using,
OrResult(msg.StatusCode == System.Net.HttpStatusCode.RequestTimeout), etc.
Here is the policy.
// Create the retry policy we want
var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.RequestTimeout)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
Recall that Polly makes your HTTP calls more robust and resilient by streamlining the implementation of retry logic.
Customize Polly policy as you want
The number of tries, the intervals between delays, and other settings can be changed to suit the needs of your application.
You can also log errors and carry out additional tasks inside the policy.
If you wanted to learn more about the Polly, then please refer to the below link, which mentions everything about how to make Polly policies and use those policies in our application.
https://github.com/App-vNext/Polly
We learned the new technique and evolved together.
Happy coding :)