Reputation: 2098
I am trying to find the best way to write an extension method for 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.
Upvotes: 1
Views: 459
Reputation: 142303
You are missing async-await
in your this.RunAsync(() => Task.Run(() => (IPollyDto) TestA.GetItem(dataset)))
call. Change it to:
this.RunAsync(() => Task.Run(async () => (IPollyDto)(await TestA.GetItem(dataset))));
Or just:
this.RunAsync(async () => (IPollyDto)(await TestA.GetItem(dataset)))
Upvotes: 1