HttpClient retry mechanism with .NET Core, Polly and IHttpClientFactory

A lot of HttpClient errors are temporary and is caused by server overload, temporary nerwork timeouts and generic gliches in the Matrix. These scenarios can be dealt with using a retry pattern. In .NET Core, the most common retry library is the Polly library:

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.

Polly makes it relatively easy to implement a retry pattern, as long as you use the IHttpClient and IHttpClientFactory.

But enugh talk, lets code.


You need (at least) the following NuGet Packages:

  • Polly
  • Microsoft.Extensions.Http.Polly


In the services configuration, you need to add a IHttpClientFactory and attach a PolicyHandler to the factory:

//ConfigureServices()  - Startup.cs

private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
  return HttpPolicyExtensions
    // Handle HttpRequestExceptions, 408 and 5xx status codes
    // Handle 404 not found
	.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
    // Handle 401 Unauthorized
	.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.Unauthorized)
    // What to do if any of the above erros occur:
	// Retry 3 times, each time wait 1,2 and 4 seconds before retrying.
	.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));


The IHttpClientFactory can be injected using constructor injection. The cool part of the Polly implementation is that your HttpClient code does not contain any special retry-code, just the usual Get or Post calls:

namespace MyCode
  public class MyClass
    private readonly IHttpClientFactory _clientFactory;

    public MyClass(IHttpClientFactory clientFactory)
      _clientFactory = clientFactory;

    public async Task<int> PostMessage(string postData)
      var httpClient = _clientFactory.CreateClient("HttpClient");

      using (var content = new StringContent(postData, Encoding.UTF8, "application/json"))
        var result = await httpClient.PostAsync($"{url}", content);
        // The call was a success
        if (result.StatusCode == HttpStatusCode.Accepted)
          return result.StatusCode;
        // The call was not a success, do something
          // Do something
          return result.StatusCode;

The httpClient.PostAsync() will retry the post call automatically if any of the conditions described in the GetRetryPolicy() occurs. It will only return after the call is either successful or the retry count is met.


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .NET Core, General .NET and tagged , , . Bookmark the permalink.

4 Responses to HttpClient retry mechanism with .NET Core, Polly and IHttpClientFactory

  1. Will保哥 says:


    This line: .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));

    It should wait and retry 3 times, each time wait 2, 4 and 8 seconds before retrying. You comment is wrong.
    // Retry 3 times, each time wait 1,2 and 4 seconds before retrying.

    Liked by 1 person

  2. Pingback: Add a UserAgent to the IHttpClientFactory in .NET Core | Brian Pedersen's Sitecore and .NET Blog

  3. Pingback: HttpClient retry on HTTP timeout with Polly and IHttpClientBuilder | Brian Pedersen's Sitecore and .NET Blog

  4. Pingback: Get Users from IdentityServer using .NET Core | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.