2

I'm attempting to log something before retrying a web api call using Polly in a .net core web api.

I know the web api is failing and returning a 503 response code however there's nothing in my console log as part of the retry call. Any ideas why and how to resolve this?

var retryPolicy = Policy
    .Handle<HttpRequestException>()
    .Or<SocketException>()
    .WaitAndRetryAsync(new[]
    {
        TimeSpan.FromSeconds(2),
        TimeSpan.FromSeconds(5),
        TimeSpan.FromSeconds(10)
    }, (exception, timeSpan, retryCount, context) =>
    {
        Console.Write("RETRYING - " + DateTime.Now.Second);
    });

await retryPolicy.ExecuteAsync(async () =>
{
    var serviceReturnLabel = await this.stockTransfersServiceClient.GetPRReturnLabel(ItemSourceType.ReturnLocker);

    if (serviceReturnLabel != null && serviceReturnLabel.Accepted)
    {
        returnLabel = serviceReturnLabel.PRLabel;
    }
});
6
  • The onRetryAsync is the right delegate. So, your Console.Write is in the right place. Have your tried to replace the Console.Write to a logger? Commented Jun 24, 2021 at 9:30
  • @PeterCsala Yep, I was first attempting to write to my own logger but that didn't work so tried writing to console. Commented Jun 24, 2021 at 9:34
  • Have you verified that delegate has been called during debugging by setting there a breakpoint? Commented Jun 24, 2021 at 9:36
  • 1
    If it does not get called that could mean 2 things: 1 - The exception is unhandled from the policy perspective (so neither HttpRequestException nor SocketException is thrown rather some other kind of exception). 2 - There was no exception at all Commented Jun 24, 2021 at 9:48
  • 1
    @PeterCsala Cheers. I handled the custom response object instead of a HttpRequestException. Not sure if you want to write an answer or shall I delete the question? Commented Jun 24, 2021 at 10:09

1 Answer 1

3

The retry policy exposes a hook where you can wire up a custom code which will be called before the retry penalty. In other words this delegate will be called whenever the policy should be triggered but before the wait between the two attempts.

This hook is called onRetry or onRetryAsync depending whether your policy is sync or async respectively.

Here you can see when will these user defined custom delegates be called:


So, you have wired up to the right hook.

Now you have to make sure that policy is triggered. You can use Console.Write or some logger to push information from your delegate to the standard output.

Or you can simply set a breakpoint in your anonymous lambda delegate to make sure that it is called during debugging.


If it is not called then you have to check the following:

  • Are the thrown exception handled?
  • Is there any exception at all?

From a policy perspective there can be two kinds of exceptions: handled and unhandled. The former can trigger a new attempt if the threshold is not reached yet. The latter won't trigger another attempt rather it will re-throw the original exception. (Reference)

In your case the policy has been setup to trigger either when a HttpRequestException is thrown or when a SocketException. If the thrown exception is none of these then it is considered unhandled from the policy perspective.

Your policy won't be triggered if there was no exception. There is one typical mistake that we have made several times. Let's suppose we expect that the http response should be 200. Whenever is not success then we want to issue a retry. We might utilize the HandleTransientHttpError (Ref) extension. But that extension watches only the 408 and 5xx status codes. So if we receive for example 429 (too many requests) then no retry will happen. We have to explicitly call the EnsureSuccessStatusCode (Ref) method to throw error if the response was not successful.

Sign up to request clarification or add additional context in comments.

Comments

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.