Unable to get Async Retry Policy to work
See original GitHub issueCan 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.
- 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;
}
}
- 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:
- Created 6 years ago
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@duydle -
what is the exact type of the exception? I’m not clear on what a “memory heap” exception is. What is the exception message?
I assume
dbConnection.QueryAsync<T>
returns aTask
? Do you need toawait
that call?