How to write retry policy in .net
I have the below code which is called by a webhook. It does a post call to a cms which has a rate limiting policy where it allows only 10 calls per second. So how to write a retry policy the api calls fails.
4 Replies
Any idea on how to start
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
So is this policy should be handled in the startup. I already have a retry policy has been added for timeout. I can't able to see the class ResiliencePipeline eventhough I have polly in my .net core. Seems like I have Microsoft.extension.http.polly. Is both are different.
services
.AddHttpClient<IContentfulManagementService, ContentfulManagementService>((s, c) =>
{
c.BaseAddress = new Uri(managementApiURL);
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ManagementClientApiKey);
c.Timeout = timeout;
})
.AddTransientHttpErrorPolicy(ErrorPolicy)
.AddPolicyHandler(timeoutPolicy);
private static Func<PolicyBuilder<HttpResponseMessage>, IAsyncPolicy<HttpResponseMessage>> ErrorPolicy =>
p => p.Or<TimeoutRejectedException>().WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));