Permalink
Cannot retrieve contributors at this time
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?
resilience-strategy-with-polly/src/Resilience.Polly.Abstractions/PolicyAsyncExecutor.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
48 lines (41 sloc)
1.59 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |