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.Polly
builder.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:

TL; DR: You just have to change the message handlers registration order
from
to
UPDATE #1
In order to better understand why
DelegatingHandlers' registration order matter I will extend my original post.The
AddPolicyHandlerThis extension method basically registers a
PolicyHttpMessageHandlerinstance into the handlers pipeline. This class implements theDelegatingHandlerabstract class in a way that it applies a policy to the outgoing request. That's why the policy's type should beIAsyncPolicy<HttpResponseMessage>.The important stuff here is that from the
HttpClientperspective yourLogHandlerand thisPolicyHttpMessageHandlerare treated in the same way.AddHttpMessageHandler,AddPolicyHandlerIf you register the handlers into the HttpClient's pipeline like you did then the output will be:
Lets see what happens under the hood
I've used mermaid.js to draw this diagram and I've used autonumbering to be able to correlate action and emitted output.
AddPolicyHandler,AddHttpMessageHandlerThis time lets switch the registration order. The output will be:
The actions sequence:
and finally the correlated log:
For more details please check out this SO topic.
Here you can find a dotnet fiddle to be able to play with it.