I have a Named Client with the DefaultRequestHeaders being added at configuration level like so:
services.AddTransient<ValidateHeaderHandler>();
services.AddHttpClient("WeatherService", client =>
{
client.BaseAddress = new Uri("http://localhost:abcde/");
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Authorization", GetNewAccessToken());
})
.AddPolicyHandler(retryPolicy)
.AddHttpMessageHandler<ValidateHeaderHandler>();
Added to the client is a DelegatingHandler. As I step through the override in ValidateHeaderHandler, which inherits from DelegatingHandler:
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (!request.Headers.Contains("Authorization"))
{ ... }
return await base.SendAsync(request, cancellationToken);
}
The header will contain "Authorization" only on the first attempt of base.SendAsync(). After that execution, if Polly's retry policy is triggered, the ValidateHeaderHandler's override of SendAsync will execute again - as expected - but all header information is completely gone.