[英]Extension Method for Polly Policy
I am trying to find the best way to write an extension method for Polly Policy我正在尝试找到为 Polly Policy 编写扩展方法的最佳方法
I have the following我有以下
public static async Task<IPollyDto> RunAsync(this IPolicy dtoClass, Func<Task<IDto>> action)
{
return
await Policy
.Handle<ApiException>(ex => ex.StatusCode == HttpStatusCode.RequestTimeout)
.RetryAsync(3,
async (exception, retryCount) => await Task.Delay(200))
.ExecuteAsync(async () => await action.Invoke().ConfigureAwait(false))
.ConfigureAwait(false);
}
public interface IPollyDto {}
public interface IPolicy {}
Then I call the code as follows然后我调用代码如下
public class DtoTest : IPollyDto
{
}
public class TestA
{
public static async Task<DtoTest> GetItem(string datasetName)
{
return await Task.Run(() => new DtoTest()) ;
}
}
public class PollyTest : IPolicy
{
public async Task<DtoTest> TestMe(string dataset)
{
return (DtoTest) await this.RunAsync(() => Task.Run(() => (IPollyDto) TestA.GetItem(dataset)));
}
}
I get an error resolving types.解析类型时出现错误。
You are missing async-await
in your this.RunAsync(() => Task.Run(() => (IPollyDto) TestA.GetItem(dataset)))
call.您在this.RunAsync(() => Task.Run(() => (IPollyDto) TestA.GetItem(dataset)))
调用中缺少async-await
。 Change it to:将其更改为:
this.RunAsync(() => Task.Run(async () => (IPollyDto)(await TestA.GetItem(dataset))));
Or just:要不就:
this.RunAsync(async () => (IPollyDto)(await TestA.GetItem(dataset)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.