C
C#10mo ago
Raki

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.
c#
public async Task HandleRedirectAsync(string slug, string previousSlug, string contentTypeId, string entryId)
{
var endpoint = "entries";
if (!string.IsNullOrEmpty(slug))
{
var isPreviousSlugNotEmpty = !String.IsNullOrEmpty(previousSlug);
var isSlugAndPreviousSlugSame = slug == previousSlug;


if (isPreviousSlugNotEmpty && isSlugAndPreviousSlugSame)
{
return;
}

await DeleteRedirectLoopsAsync(slug, contentTypeId, endpoint);


if (isPreviousSlugNotEmpty && !isSlugAndPreviousSlugSame)
{
await CreateAndPublishRedirectEntryAsync(entryId, previousSlug, slug);
}
await UpdateEntryAsync(contentTypeId, entryId);
}
}
c#
public async Task HandleRedirectAsync(string slug, string previousSlug, string contentTypeId, string entryId)
{
var endpoint = "entries";
if (!string.IsNullOrEmpty(slug))
{
var isPreviousSlugNotEmpty = !String.IsNullOrEmpty(previousSlug);
var isSlugAndPreviousSlugSame = slug == previousSlug;


if (isPreviousSlugNotEmpty && isSlugAndPreviousSlugSame)
{
return;
}

await DeleteRedirectLoopsAsync(slug, contentTypeId, endpoint);


if (isPreviousSlugNotEmpty && !isSlugAndPreviousSlugSame)
{
await CreateAndPublishRedirectEntryAsync(entryId, previousSlug, slug);
}
await UpdateEntryAsync(contentTypeId, entryId);
}
}
4 Replies
Raki
Raki10mo ago
Any idea on how to start
Pobiega
Pobiega10mo ago
The defacto standard way would be to use Polly.
// Create an instance of builder that exposes various extensions for adding resilience strategies
ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()) // Add retry using the default options
.AddTimeout(TimeSpan.FromSeconds(10)) // Add 10 seconds timeout
.Build(); // Builds the resilience pipeline

// Execute the pipeline asynchronously
await pipeline.ExecuteAsync(static async token => { /* Your custom logic goes here */ }, cancellationToken);
// Create an instance of builder that exposes various extensions for adding resilience strategies
ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()) // Add retry using the default options
.AddTimeout(TimeSpan.FromSeconds(10)) // Add 10 seconds timeout
.Build(); // Builds the resilience pipeline

// Execute the pipeline asynchronously
await pipeline.ExecuteAsync(static async token => { /* Your custom logic goes here */ }, cancellationToken);
you just create a pipeline with your selected strategies (like retry with timeout) then execute it, passing in your method
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Raki
Raki10mo ago
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)));
Want results from more Discord servers?
Add your server