What to return as onRetryAsync?

会有一股神秘感。 提交于 2020-04-17 17:56:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!