In short: I want to execute X-HttpMessageHandlers when retrying a request.
Implementation: Currently, I have added an HttpClient, whose request gets handled by a Logging- and a PolicyHandler:
using Microsoft.Extensions.Http.Pollybuilder.Services.AddHttpClient() .AddHttpMessageHandler<LoggingHandler>() .AddPolicyHandler(GetRetryPolicy());
Policy: To keep the policy very simple, let's use an example:
public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(){ // HandleTransientHttpError: Handles HttpRequestException, // Http status codes >= 500 (server errors) // and status code 408 (request timeout) return HttpPolicyExtensions .HandleTransientHttpError() .RetryAsync(3); // Retry the request up to 3 times}
In case you want to test out the retry policy, send requests to: https://httpbin.org/status/503.
The above link will return an unsuccessful HttpResponseMessage with a status code 502, aka Bad Gateway. This response type is a server error, so it will get handled by the policy.
Sources: