Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Polly;
using Polly.Registry;
namespace Resilience.Polly.Abstractions
{
public class PolicyAsyncExecutor : IPolicyAsyncExecutor
{
public PolicyRegistry PolicyRegistry { get; set; }
public PolicyAsyncExecutor(IEnumerable<IAsyncPolicy> policies)
{
var asyncPolicies = policies ?? throw new ArgumentNullException(nameof(policies));
PolicyRegistry = new PolicyRegistry
{
[nameof(PolicyAsyncExecutor)] = Policy.WrapAsync(asyncPolicies.ToArray())
};
}
public async Task<T> ExecuteAsync<T>(Func<Task<T>> action)
{
var policy = PolicyRegistry.Get<IAsyncPolicy>(nameof(PolicyAsyncExecutor));
return await policy.ExecuteAsync(action);
}
public async Task ExecuteAsync(Func<Task> action)
{
var policy = PolicyRegistry.Get<IAsyncPolicy>(nameof(PolicyAsyncExecutor));
await policy.ExecuteAsync(action);
}
public async Task<T> ExecuteAsync<T>(Func<Context, Task<T>> action, Context context)
{
var policy = PolicyRegistry.Get<IAsyncPolicy>(nameof(PolicyAsyncExecutor));
return await policy.ExecuteAsync(action, context);
}
public async Task ExecuteAsync(Func<Context, Task> action, Context context)
{
var policy = PolicyRegistry.Get<IAsyncPolicy>(nameof(PolicyAsyncExecutor));
await policy.ExecuteAsync(action, context);
}
}
}