question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to get Async Retry Policy to work

See original GitHub issue

Can someone see what I’m doing wrong below when implementing Async Retry Policy. This is causing a memory heap exception that crashes my IIS application pool.

  1. I have an Async wrapper method using Dapper.QueryAsync<T>
    public static async Task<IEnumerable<T>> QueryAsyncWithRetry<T>(IDbConnection dbConnection, string sql, CommandType commandType, DynamicParameters paramList, int maxRetries = 3, int commandTimeout = 120)
        {
            try
            {
                //Need to verify retry when method is implemented
                var result = await (Task<IEnumerable<T>>)RetryPolicy.RetryForTimeoutAsync<T>(new Func<
                                                 string,
                                                 object,
                                                 IDbTransaction,
                                                 int?,
                                                 CommandType?,
                                                 Task<IEnumerable<T>>>(dbConnection.QueryAsync<T>),
                                                 maxRetries,
                                                 sql,
                                                 paramList,
                                                 null,
                                                 commandTimeout,
                                                 commandType
                                                 );
                return result;
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex, $"DapperExtenions.QueryAsyncWithRetry<T> failed on {sql}");
                throw;
            }
        }
  1. I create Policy using RetryAsync and ExecuteAsync.
    public static async Task<IEnumerable<T>> RetryForTimeoutAsync<T>(Delegate method, int maxRetries = 3, params object[] args)
        {
            var policy = Policy
                        .Handle<Exception>(ex => ExceptionMiner.FullMessageChain(ex).ToLower().Contains(timeout))
                        .Or<Exception>(ex => ExceptionMiner.FullMessageChain(ex).ToLower().Contains(deadlock))
                        .RetryAsync(maxRetries, (ex, retryCount) =>
                        {
                            _logger.Error(ex, $"Attempting retry {retryCount} of maxRetry {maxRetries} for timeout/deadlock on method {method.Method.Name}.  Parameter List Info: {GetPropertyValues(args)}");
                        });

            try
            {
                var result = await policy.ExecuteAsync(() => (Task<IEnumerable<T>>) method.DynamicInvoke(args));
                return result;
            }
            catch (Exception e)
            {
                //log the exception after maxRetries are reached or there is no retry exception being handled
                _logger.Fatal(e, $"RetryPolicy.Retry failed on method {method.Method.Name}. Parameter List Info: {GetPropertyValues(args)}");
                throw;
            }
        }

( EDITED: by @reisenberger to format code for readability. )

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
duydlecommented, Dec 18, 2017

Thanks all for the response. At the moment, let’s close this ticket since I won’t be able spend too much time on this issue. For now we will not implement a retry on async calls. If I have availability, I will circle back and try to create a reproducible code sample and send your way. Thanks all for the quick responses.

1reaction
udlosecommented, Dec 18, 2017

@duydle -

  1. what is the exact type of the exception? I’m not clear on what a “memory heap” exception is. What is the exception message?

  2. I assume dbConnection.QueryAsync<T> returns a Task? Do you need to await that call?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Retry policy is not working when opening connection ...
Just dig into source and found that I get an SQL Error which is not transient. My error code was 10065 and error...
Read more >
"Unable to cast object of type Polly.Retry.RetryPolicy ...
After defining a policy and getting it registered with registry ... Retry.RetryPolicy to type Polly.Async Policy" exception is raised #493.
Read more >
question: Retry policy with action upon failure #266
I'm using a delay and retry policy, something like this: var policy ... We'll aim to fix up the use of ExecuteAndCapture/Async() with ......
Read more >
Use Polly to retry requests in C# - Duong's Blog
The Polly library is my go-to choice whenever I need to retry my HTTP requests. In today's article, we will see how it...
Read more >
Implementing the retry pattern in c sharp using Polly
One Gotcha I found - if you use the Policy.ExecuteAsync asynchronous execution API, you must use the WaitAndRetryAsync policy creation method ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found