0

I am trying to use Polly to wrap an async method. How I get an error with my method, I try this:

var myResult = await _politicaReintentar.ExecuteAsync(async () => await Task.Delay(1000));

But I get an error that tells it cannot convert async lambda expression to delegate Task<HttpResponseMessage>, and the error is marked in await Task.Delay(1000).

In the documentation the way to use is this:

var response = await timeoutPolicy
    .ExecuteAsync(
      async () => await FooNotHonoringCancellationAsync(), // Execute a delegate which takes no CancellationToken and does not respond to cancellation.
      );

I don't see the problem, I use async and await as parameters of ExecuteAsync.

How should I call my async method?

Thanks.

EDIT: this is the policy:

_politicaReintentar = HttpPolicyExtensions
    .HandleTransientHttpError() // HttpRequestException, 5XX and 408
    .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt));
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

3

Based on the so far provided information, I assume you have defined a policy where the to-be-decorated async method should return an HttpReponseMessage.

Task.Delay does not return with a HttpResponseMessage. Your policy definition must be compatible with the to-be-decorated method return type.


UPDATE #1

I really would like to learn how to execute an async method with Polly and retry 3 times, for example. In this case, a basic case is to execute a Task.Delay()

I don't want to repeat here the documentation of Polly so, let me give you some really short summary.

In case of Polly the policy definition and its execution are separated.

Definition

The definition is done via several builder functions. In all cases you have to define the following three:

  • The to-be-decorated action is sync/async and method/function
  • The trigger(s) for which the policy should be fire
  • The chosen resilience action and its configuration

In case of your _politicaReintentar these are the following:

HttpPolicyExtensions.HandleTransientHttpError()

It is equivalent with the following:

Policy<HttpResponseMessage>
  .Handle<HttpRequestException>()
  .OrTransientHttpStatusCode()
  • The to-be-decorated function should return a HttpResponseMessage
  • It should trigger either for HttpRequestException
  • Or it should trigger if the status code is 408 or 5xx
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt));
  • It says that the to-be-decorated method is async
  • The retry policy should be used as a chosen resiliency action
  • With at most 3 retry attempts, with predefined sleep duration between attempts

If you are interested more about this, please check the following SO topics:

Execution

Depending on the policy definition you can either call Execute or ExecuteAsync. So, you have to know upfront what do you want to decorate and define your policy accordingly.

Polly also exposes methods like ExecuteAndCapture and ExecuteAndCaptureAsync. The different between these and the above ones is that the latter do not throw exception rather captures either the result of the method or the exception into a PolicyResult object.

Back to your example

Retrying the Task.Delay unconditionally is not supported by Polly. You have to have either a Handle<> or HandleResult<> to define a trigger. For the sake of simplicity I will cancel the Task.Delay which will throw OperationCanceledException and that would be our trigger.

var retry = Policy
    .Handle<OperationCanceledException>()
    .RetryAsync(3, (_, __) => Console.WriteLine("Retried"));
await retry.ExecuteAsync(async () =>
{
     var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
     await Task.Delay(2000, cts.Token);
});

I've added some logging to the policy for clarity.

Here you can find the related dotnet fiddle link.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Thanks for the reply. But I don't know how to do it. Could you show an example? – Álvaro García Mar 21 '23 at 15:47
  • @ÁlvaroGarcía What exactly do you want to achieve? Use your policy as suppose to? Define a policy for Task.Delay? Or something else? – Peter Csala Mar 21 '23 at 16:32
  • I really would like to learn how to execute an async method with Polly and retry 3 times, for example. In this case, a basic case is to execute a Task.Delay(). – Álvaro García Mar 21 '23 at 16:38
  • Well, when I use Policy like in your example, it works, but when I use HttpPolicyExtensions, how I indicate in my post, it is when I get the error. So how could I configure HttpPolicyExtensions to can use HandleTransientHttpError? – Álvaro García Mar 21 '23 at 18:44
  • @ÁlvaroGarcía The HandleTransientHttpError is suitable to decorate an HttpClient instance. So, that should be used to decorate idempotent network requests. Is it clear? – Peter Csala Mar 21 '23 at 19:14
  • Does it means that I can't use HandleTransientHttpError to execute a method that doesn't return HttpResponseMessage? If this is true, how could I handle the transient exceptions with polly? With Handle()? – Álvaro García Mar 22 '23 at 09:21
  • @ÁlvaroGarcía The `HandleTransientHttpError` is a predefined policy for common HttpClient based transient errors. You can extend that with `Or()` to trigger for any exception if you wish. – Peter Csala Mar 22 '23 at 09:42
  • Then if I understand correctly, I should to Policy.Handle().Or.Or()...? – Álvaro García Mar 22 '23 at 11:55
  • @ÁlvaroGarcía Yes, you can do that or you can provide there a predicate function like this `.Handle(ex => ShouldTriggerFor(ex))` or in short `.Handle(ShouldTriggerFor)`. The `ShouldTriggerFor` could check the exception type and return true if should be retry should be triggered otherwise false. – Peter Csala Mar 22 '23 at 12:23