We started using the Polly library in our legacy WinForms project which still runs on the .NET 4.0 framework (is a requisite).
The problem is that we have to use version 4.3 of the Polly library and it is difficult to find solutions to the problems because all the documentation we find is about more recent versions of the library.
For example we can't pass Context
values from retry callback to execute because Context
is readonly and we can't pass parameters to execute delegate because it use an Action
type.
To all these problems we have find a creative solution but we can't still found a way to stop the execution on certain condition.
In Polly 5, CancellationToken
was introduced for this purpose but I guess there were ways to force the retry to stop in previous versions as well.
public RetryPolicy DevicePolicy => Policy
.Handle<Exception>()
.WaitAndRetry(
MaxRetries,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
(exception, timeSpan, retryCount, context) =>
{
//If i get the timeout exception i want to stop the execution
if (exception is TimeoutException)
{
//In Polly 5.0 I can set the cancellationToken but with 4.3 there isn't
var cts = context["CancellationTokenSource"] as CancellationTokenSource;
cts.Cancel();
}
else
{
var errHeader = $"device connection error. Attempt {retryCount} of {MaxRetries}";
Log.Warn(errHeader, exception);
}
});
Any Idea?