1

How can I add a Polly retry policy to the AspNetCore.HealthChecks.Uris health check?

This health check is part of this repo: https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks

I see that I can use the configureClient and configurePrimaryHttpMessageHandler call backs when using healthCheckBuilder.AddUrlGroup.

However all the examples I see for using Polly require using IClientBuilder.AddPolicyHandler(...), but I can't figure out how to access IHttpClientBuilder when using healthCheckBuilder.AddUrlGroup.

4
  • What exactly do you want to achieve with the retry? Commented Jan 25 at 15:50
  • I want the health check to try the URL more than once before reporting a failure. Commented Jan 25 at 17:09
  • As far as I know, the health checks endpoints are called periodically. Because of this, the main aim here is to quickly determine whether the given resource is available or not. After N consecutive failures you can declare temporal unavailability of the resource. So, according to my understanding, adding retry to health checks does not give you extra safety. Commented Jan 25 at 18:40
  • Thanks, health checks can be polled periodically by using the publish functionality. Other than that, I don't think that they are. I could be wrong though. Commented Jan 25 at 22:12

1 Answer 1

1

The configurePrimaryHttpMessageHandler as its name suggests anticipates an HttpMessageHandler as a return value. This abstract class serves as a base class for many derived classes like DelegatingHandler or HttpClientHandler. Gladly Polly's HttpClient integrations are built upon DelegatingHandlers.

Polly V7

The AddPolicyHandler registers a PolicyHttpMessageHandler which is a DelegatingHandler. So, you can configure this class inside the configurePrimaryHttpMessageHandler callback.

.ConfigurePrimaryHttpMessageHandler(() =>
{
  return new PolicyHttpMessageHandler(policy)
  {
    InnerHandler = new HttpClientHandler()
  };
});

Polly V8

Similarly, the AddResilienceHandler registers a ResilienceHandler which is a DelegatingHandler.

.ConfigurePrimaryHttpMessageHandler(() =>
{
  return new ResilienceHandler(pipeline)
  {
    InnerHandler = new HttpClientHandler()
  };
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is a fantastic response. Thanks for taking the time to answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.