Search code examples
c#.netpollyretry-logic

Polly and async method: cannot convert async lambda expression to delegate Task<HttpResponseMessage>


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));

Solution

  • 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.