Reputation: 57
I'm having a problem with Polly causing the thread to freeze. As you can see under the GetData() method is going to throw an exception. I want to do is to continue to Dostuff1 and 2 when exceptions occur in the GetData() method in DoStuff(); But it seems like after retrying with polly it is just stuck and doing nothing at all...
How can I fix this?
private async Task StartUp()
{
await DoStuff());
// await next task DoStuff1();
// await next task DoStuff2();
}
private async Task DoStuff()
{
var data= await Task.WhenAll(Task.Run(() => GetData()));
// Do stuff with the data
}
private async Task<Data> GetData()
{
//read filepaths from file.
var tasks = filePaths
.Select(f => polly.MyPolicy().Execute(() => ReadDataFromCSV()))
.ToArray();
Return await tasks;
}
private Dictionary<string, DateTime?> ReadDataFromCSV()
{
///Throw exception....
}
public PolicyForGetData()
{
return Policy
.Handle<Exception>()
.WaitAndRetry(
retryCount: 2,
sleepDurationProvider: t => TimeSpan.FromSeconds(5),
onRetry: (ex, t, i, c) => {
Console.WriteLine(ex.Message);
}
);
}
Upvotes: 1
Views: 1109
Reputation: 3451
I think you're misunderstanding how Polly behaves. Assuming GetData()
always throws then your application will work like this
StartUp()
method which prevents DoStuff1()
and DoStuff2()
from being called.For example. The output of this program:
class Program
{
static void Main(string[] args)
{
try
{
Policy
.Handle<Exception>()
.WaitAndRetry(
retryCount: 2,
sleepDurationProvider: t => TimeSpan.FromSeconds(5),
onRetry: (ex, t, i, c) => {
Console.WriteLine("OnRetry");
}
)
.Execute(() => ErrorMethod());
Console.WriteLine("We never get here");
}
catch (Exception)
{
Console.WriteLine("Exception Handler");
}
}
private static void ErrorMethod()
{
throw new InvalidOperationException("Badness");
}
}
Is
OnRetry
OnRetry
Exception Handler
Upvotes: 1