0

I am referring this https://github.com/Pathoschild/FluentHttpClient#custom-retry--coordination to create a custom retry coordination and trying to use Polly here, but I am getting below error,

'PolicyBuilder< HttpResponseMessage>' does not contain a definition for 'Retry' and the best extension method overload 'RetrySyntax.Retry(PolicyBuilder, int, Action)' requires a receiver of type 'PolicyBuilder'

What wrong here?

public class PollyCoordinator : IRequestCoordinator
{
    public Task<HttpResponseMessage> ExecuteAsync(IRequest request, Func<IRequest, Task<HttpResponseMessage>> dispatcher)
    {
        int[] retryCodes = { 408, 500, 502, 503, 504 };
        return Policy
            .HandleResult<HttpResponseMessage>(r => retryCodes.Contains((int)r.StatusCode))
            .Retry(3, async () => await dispatcher(request));
    }
}
user584018
  • 10,186
  • 15
  • 74
  • 160
  • 1
    Do you have `using Polly;` at the top of your file? Also, I don't think you can return the `Retry` by itself because that method returns a `PolicyBuilder` object. You have to use `Retry().Execute()`. Lastly, why isn't this method marked as `async` so you use `RetryAsync()`? – howcheng Nov 14 '19 at 22:06

1 Answers1

1

I updated the example in the FluentHttpClient readme. Here's a fixed version of your code:

public class PollyCoordinator : IRequestCoordinator
{
    public Task<HttpResponseMessage> ExecuteAsync(IRequest request, Func<IRequest, Task<HttpResponseMessage>> send)
    {
        int[] retryCodes = { 408, 500, 502, 503, 504 };
        return Policy
            .HandleResult<HttpResponseMessage>(r => retryCodes.Contains((int)r.StatusCode)) // should we retry?
            .RetryAsync(3) // up to 3 retries
            .ExecuteAsync(() => send(request)); // begin handling request
    }
}

If you want a delay between each retry (recommended), you can replace RetryAsync(3) with something like WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(attempt)).

Pathoschild
  • 4,636
  • 2
  • 23
  • 25