问题
I am trying to implement a retry policy that will retry if an exception is thrown.
Unfortunately I can't seem to get the signature for the onRetryAsync block right. The compiler says "Not all code paths return a value in lambda expression of type...."
The documentation suggests to return Task.CompletedTask but that's apparently not available to me in the current libraries I am forced to use.
var retryPolicy = Policy
.Handle<SigsThrottledException>(e => e.RetryAfterInSeconds > 0)
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: (i, e, ctx) =>
{
var ste = (SigsThrottledException)e;
return TimeSpan.FromSeconds((double)ste.RetryAfterInSeconds);
},
onRetryAsync: (e, ts, i, ctx) =>
{
// Logging goes here
});
<....>
var response = await retryPolicy.Execute(async () =>
{
Uri substrateurl = new Uri("https://substrate.office.com/");
return await SIGSClient.Instance.PostAsync(client, substrateurl, new UserInfo(), "faketoken", new Signal(), Guid.NewGuid()).ConfigureAwait(false);
}
);
回答1:
So: This is a case of the compiler being less than helpful, compounded by the fact that async/await etc. is still fairly new to me, and not always that easy to figure out.
Essentially I was missing one thing:
onRetryAsync: async (e, ts, i, ctx) =>
...an async in front of the signature, which by the way was not present in the code examples I was linking to.
The type of the onRetryAsync argument is Func<Exception, TimeSpan, int, Context, Task>
and one can be declared like this:
Func<Exception, TimeSpan, int, Context, Task> nopBlock = async (e, ts, i, ctx) =>
{
// Do something here
// The "something" should be async
};
I eventually figured this out by looking up the signature of the WaitAndRetryAsync overload that I was calling.
来源:https://stackoverflow.com/questions/60607103/what-to-return-as-onretryasync