Question

Migration from Polly to Microsoft.Extensions.Http.Resilience - Extend ShouldHandle

I want to migrate from Polly to the Microsoft.Extensions.Http.Resilience AddStandardResilienceHandler. My shortened Polly code is the following:

services.AddHttpClient<MyService>()
    .AddPolicyHandler((_, _) =>
    {
        return HttpPolicyExtensions.HandleTransientHttpError()
            .Or<HttpRequestException>(exception => exception.StatusCode == HttpStatusCode.Conflict)
            .WaitAndRetryAsync(3, sleepDurationProvider: i => TimeSpan.FromSeconds(i * 2));
    });

The crucial part here is HttpPolicyExtensions.HandleTransientHttpError().Or<HttpRequestException>(exception => exception.StatusCode == HttpStatusCode.Conflict) where I want to add a custom error case in which I want to do a retry.

I do not really know how to convert this into the new configuration.

As far as I understand the docs I can set a custom options.Retry.ShouldHandle function (Gets or sets a predicate that determines whether the retry should be executed for a given outcome.)

But then I cannot add a case rather than I have to set the whole function. Microsofts default implementation of ShouldHandle looks like this:

public static readonly Func<TArgs, ValueTask<bool>> HandleOutcome = args => args.Outcome.Exception switch
{
    OperationCanceledException => PredicateResult.False(),
    Exception => PredicateResult.True(),
    _ => PredicateResult.False()
};

My current config looks like this:

services.AddHttpClient<MyService>()
    .AddStandardResilienceHandler()
    .Configure((options, _) =>
    {
        options.Retry.ShouldHandle = args =>
        {
            if (args.Outcome.Result?.StatusCode == HttpStatusCode.Conflict)
            {
                return PredicateResult.True();
            }

            return args.Outcome.Exception switch
            {
                OperationCanceledException => PredicateResult.False(),
                not null => PredicateResult.True(),
                _ => PredicateResult.False()
            };
        };
    });

Is this equivalent to the initial Polly implementation?

 2  52  2
1 Jan 1970

Solution

 2

The original HandleTransientHttpError could be migrated to V8 like this:

public static class PollyUtils
{
  public static ValueTask<bool> HandleTransientHttpError(
    Outcome<HttpResponseMessage> outcome)
    => outcome switch
    {
        { Exception: HttpRequestException } => PredicateResult.True(),
        { Result.StatusCode: HttpStatusCode.RequestTimeout} => PredicateResult.True(),
        { Result.StatusCode: >= HttpStatusCode.InternalServerError } => PredicateResult.True(),
        _ => PredicateResult.False()
    };
}

The default value of ShouldHandle is defined to trigger for every exception except OperationCanceledException. If you have cancelled an operation then you might not want to retry it.

This default value might not make sense for every use case. For instance HttpClient can throw InvalidOperationException in many cases:

  • if the request body has already sent and you want to resend it
  • or if you try to set the BaseAddress or Timeout properties after a request has been sent out
  • etc.

Retrying on these won't change the outcome of the operation.

The above utility function can be easily extended to trigger for Conflict as well.


By the way the AddStandardResilienceHandler registers a Retry strategy (among other strategies). That retry's ShouldHandle triggers for the following things:

  • HttpRequestException
  • TimeoutRejectedException
  • greater or equals to HttpStatusCode.InternalServerErrorCode (5XX)
  • HttpStatusCode.RequestTimeout (408)
  • HttpStatusCode.TooManyRequests (429)
2024-07-19
Peter Csala