<PackageReference Include="Polly" Version="5.9.0" />

Policy

public abstract class Policy : PolicyBase, IAsyncPolicy, IsPolicy, ISyncPolicy
Transient exception handling policies that can be applied to delegates
using Polly.Bulkhead; using Polly.Caching; using Polly.NoOp; using Polly.Timeout; using Polly.Utilities; using Polly.Wrap; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Polly { public abstract class Policy : PolicyBase, IAsyncPolicy, IsPolicy, ISyncPolicy { private readonly Action<Action<Context, CancellationToken>, Context, CancellationToken> _exceptionPolicy; private readonly Func<Func<Context, CancellationToken, Task>, Context, CancellationToken, bool, Task> _asyncExceptionPolicy; public static BulkheadPolicy Bulkhead(int maxParallelization) { Action<Context> onBulkheadRejected = delegate { }; return Bulkhead(maxParallelization, 0, onBulkheadRejected); } public static BulkheadPolicy Bulkhead(int maxParallelization, Action<Context> onBulkheadRejected) { return Bulkhead(maxParallelization, 0, onBulkheadRejected); } public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActions) { Action<Context> onBulkheadRejected = delegate { }; return Bulkhead(maxParallelization, maxQueuingActions, onBulkheadRejected); } public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActions, Action<Context> onBulkheadRejected) { if (maxParallelization <= 0) throw new ArgumentOutOfRangeException("maxParallelization", "Value must be greater than zero."); if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException("maxQueuingActions", "Value must be greater than or equal to zero."); if (onBulkheadRejected == null) throw new ArgumentNullException("onBulkheadRejected"); SemaphoreSlim maxParallelizationSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization); int maxParallelization2 = (maxQueuingActions <= 2147483647 - maxParallelization) ? (maxQueuingActions + maxParallelization) : 2147483647; SemaphoreSlim maxQueuedActionsSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization2); return new BulkheadPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { BulkheadEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, onBulkheadRejected, maxParallelizationSemaphore, maxQueuedActionsSemaphore, cancellationToken); }, maxParallelization, maxQueuingActions, maxParallelizationSemaphore, maxQueuedActionsSemaphore); } public static BulkheadPolicy BulkheadAsync(int maxParallelization) { Func<Context, Task> onBulkheadRejectedAsync = (Context _) => TaskHelper.EmptyTask; return BulkheadAsync(maxParallelization, 0, onBulkheadRejectedAsync); } public static BulkheadPolicy BulkheadAsync(int maxParallelization, Func<Context, Task> onBulkheadRejectedAsync) { return BulkheadAsync(maxParallelization, 0, onBulkheadRejectedAsync); } public static BulkheadPolicy BulkheadAsync(int maxParallelization, int maxQueuingActions) { Func<Context, Task> onBulkheadRejectedAsync = (Context _) => TaskHelper.EmptyTask; return BulkheadAsync(maxParallelization, maxQueuingActions, onBulkheadRejectedAsync); } public static BulkheadPolicy BulkheadAsync(int maxParallelization, int maxQueuingActions, Func<Context, Task> onBulkheadRejectedAsync) { if (maxParallelization <= 0) throw new ArgumentOutOfRangeException("maxParallelization", "Value must be greater than zero."); if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException("maxQueuingActions", "Value must be greater than or equal to zero."); if (onBulkheadRejectedAsync == null) throw new ArgumentNullException("onBulkheadRejectedAsync"); SemaphoreSlim maxParallelizationSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization); int maxParallelization2 = (maxQueuingActions <= 2147483647 - maxParallelization) ? (maxQueuingActions + maxParallelization) : 2147483647; SemaphoreSlim maxQueuedActionsSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization2); return new BulkheadPolicy((Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => BulkheadEngine.ImplementationAsync(async delegate(Context ctx, CancellationToken ct) { await action(ctx, ct).ConfigureAwait(continueOnCapturedContext); return EmptyStruct.Instance; }, context, onBulkheadRejectedAsync, maxParallelizationSemaphore, maxQueuedActionsSemaphore, cancellationToken, continueOnCapturedContext), maxParallelization, maxQueuingActions, maxParallelizationSemaphore, maxQueuedActionsSemaphore); } public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization) { Action<Context> onBulkheadRejected = delegate { }; return Bulkhead<TResult>(maxParallelization, 0, onBulkheadRejected); } public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization, Action<Context> onBulkheadRejected) { return Bulkhead<TResult>(maxParallelization, 0, onBulkheadRejected); } public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization, int maxQueuingActions) { Action<Context> onBulkheadRejected = delegate { }; return Bulkhead<TResult>(maxParallelization, maxQueuingActions, onBulkheadRejected); } public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization, int maxQueuingActions, Action<Context> onBulkheadRejected) { if (maxParallelization <= 0) throw new ArgumentOutOfRangeException("maxParallelization", "Value must be greater than zero."); if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException("maxQueuingActions", "Value must be greater than or equal to zero."); if (onBulkheadRejected == null) throw new ArgumentNullException("onBulkheadRejected"); SemaphoreSlim maxParallelizationSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization); int maxParallelization2 = (maxQueuingActions <= 2147483647 - maxParallelization) ? (maxQueuingActions + maxParallelization) : 2147483647; SemaphoreSlim maxQueuedActionsSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization2); return new BulkheadPolicy<TResult>((Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult>)((Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) => BulkheadEngine.Implementation<TResult>(action, context, onBulkheadRejected, maxParallelizationSemaphore, maxQueuedActionsSemaphore, cancellationToken)), maxParallelization, maxQueuingActions, maxParallelizationSemaphore, maxQueuedActionsSemaphore); } public static BulkheadPolicy<TResult> BulkheadAsync<TResult>(int maxParallelization) { Func<Context, Task> onBulkheadRejectedAsync = (Context _) => TaskHelper.EmptyTask; return BulkheadAsync<TResult>(maxParallelization, 0, onBulkheadRejectedAsync); } public static BulkheadPolicy<TResult> BulkheadAsync<TResult>(int maxParallelization, Func<Context, Task> onBulkheadRejectedAsync) { return BulkheadAsync<TResult>(maxParallelization, 0, onBulkheadRejectedAsync); } public static BulkheadPolicy<TResult> BulkheadAsync<TResult>(int maxParallelization, int maxQueuingActions) { Func<Context, Task> onBulkheadRejectedAsync = (Context _) => TaskHelper.EmptyTask; return BulkheadAsync<TResult>(maxParallelization, maxQueuingActions, onBulkheadRejectedAsync); } public static BulkheadPolicy<TResult> BulkheadAsync<TResult>(int maxParallelization, int maxQueuingActions, Func<Context, Task> onBulkheadRejectedAsync) { if (maxParallelization <= 0) throw new ArgumentOutOfRangeException("maxParallelization", "Value must be greater than zero."); if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException("maxQueuingActions", "Value must be greater than or equal to zero."); if (onBulkheadRejectedAsync == null) throw new ArgumentNullException("onBulkheadRejectedAsync"); SemaphoreSlim maxParallelizationSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization); int maxParallelization2 = (maxQueuingActions <= 2147483647 - maxParallelization) ? (maxQueuingActions + maxParallelization) : 2147483647; SemaphoreSlim maxQueuedActionsSemaphore = SemaphoreSlimFactory.CreateSemaphoreSlim(maxParallelization2); return new BulkheadPolicy<TResult>((Func<Func<Context, CancellationToken, Task<TResult>>, Context, CancellationToken, bool, Task<TResult>>)((Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => BulkheadEngine.ImplementationAsync<TResult>(action, context, onBulkheadRejectedAsync, maxParallelizationSemaphore, maxQueuedActionsSemaphore, cancellationToken, continueOnCapturedContext)), maxParallelization, maxQueuingActions, maxParallelizationSemaphore, maxQueuedActionsSemaphore); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string, Exception> onCacheError = null) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string, Exception> onCacheError = null) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, action, action, action, onCacheError, onCacheError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, action, action, action, onCacheError, onCacheError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); if (onCacheGet == null) throw new ArgumentNullException("onCacheGet"); if (onCacheMiss == null) throw new ArgumentNullException("onCacheMiss"); if (onCachePut == null) throw new ArgumentNullException("onCachePut"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string, Exception> onCacheError = null) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string, Exception> onCacheError = null) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, action, action, action, onCacheError, onCacheError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, action, action, action, onCacheError, onCacheError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); if (onCacheGet == null) throw new ArgumentNullException("onCacheGet"); if (onCacheMiss == null) throw new ArgumentNullException("onCacheMiss"); if (onCachePut == null) throw new ArgumentNullException("onCachePut"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); ISyncCacheProvider<TResult> cacheProvider2 = cacheProvider.For<TResult>(); RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); ISyncCacheProvider<TResult> cacheProvider2 = cacheProvider.For<TResult>(); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), ttlStrategy, cacheKeyStrategy, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); ISyncCacheProvider<TResult> cacheProvider2 = cacheProvider.For<TResult>(); RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); ISyncCacheProvider<TResult> cacheProvider2 = cacheProvider.For<TResult>(); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return Cache(cacheProvider.For<TResult>(), ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Action<Context, string, Exception> onCacheError = null) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string, Exception> onCacheError = null) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Action<Context, string, Exception> onCacheError = null) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return Cache(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy.GetCacheKey, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return Cache(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return Cache(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return Cache(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); if (onCacheGet == null) throw new ArgumentNullException("onCacheGet"); if (onCacheMiss == null) throw new ArgumentNullException("onCacheMiss"); if (onCachePut == null) throw new ArgumentNullException("onCachePut"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); return new CachePolicy<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); IAsyncCacheProvider<TResult> cacheProvider2 = cacheProvider.AsyncFor<TResult>(); RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); IAsyncCacheProvider<TResult> cacheProvider2 = cacheProvider.AsyncFor<TResult>(); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); IAsyncCacheProvider<TResult> cacheProvider2 = cacheProvider.AsyncFor<TResult>(); RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); IAsyncCacheProvider<TResult> cacheProvider2 = cacheProvider.AsyncFor<TResult>(); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider2, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); return CacheAsync(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Action<Context, string, Exception> onCacheError = null) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string, Exception> onCacheError = null) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Action<Context, string, Exception> onCacheError = null) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string, Exception> onCacheError = null) { onCacheError = (onCacheError ?? ((Action<Context, string, Exception>)delegate { })); Action<Context, string> action = delegate { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, action, action, action, onCacheError, onCacheError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { RelativeTtl ttlStrategy = new RelativeTtl(ttl); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { ITtlStrategy<TResult> ttlStrategy2 = ttlStrategy.For<TResult>(); ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy2, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { ICacheKeyStrategy instance = DefaultCacheKeyStrategy.Instance; return CacheAsync(cacheProvider, ttlStrategy, instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { return CacheAsync(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static CachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException("cacheProvider"); if (ttlStrategy == null) throw new ArgumentNullException("ttlStrategy"); if (cacheKeyStrategy == null) throw new ArgumentNullException("cacheKeyStrategy"); if (onCacheGet == null) throw new ArgumentNullException("onCacheGet"); if (onCacheMiss == null) throw new ArgumentNullException("onCacheMiss"); if (onCachePut == null) throw new ArgumentNullException("onCachePut"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); if (onCachePutError == null) throw new ArgumentNullException("onCachePutError"); return new CachePolicy<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } public static NoOpPolicy NoOpAsync() { return new NoOpPolicy((Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => NoOpEngine.ImplementationAsync(async delegate(Context ctx, CancellationToken ct) { await action(ctx, ct).ConfigureAwait(continueOnCapturedContext); return EmptyStruct.Instance; }, context, cancellationToken, continueOnCapturedContext)); } public static NoOpPolicy<TResult> NoOpAsync<TResult>() { return new NoOpPolicy<TResult>((Func<Func<Context, CancellationToken, Task<TResult>>, Context, CancellationToken, bool, Task<TResult>>)((Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => NoOpEngine.ImplementationAsync<TResult>(action, context, cancellationToken, continueOnCapturedContext))); } public static NoOpPolicy<TResult> NoOp<TResult>() { return new NoOpPolicy<TResult>((Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult>)((Func<Context, CancellationToken, TResult> func, Context context, CancellationToken cancellationToken) => NoOpEngine.Implementation<TResult>(func, context, cancellationToken))); } public static NoOpPolicy NoOp() { return new NoOpPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { NoOpEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken); }); } [DebuggerStepThrough] public Task ExecuteAsync(Func<Task> action) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<Task> action, IDictionary<string, object> contextData) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<Task> action, Context context) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<Context, Task> action, IDictionary<string, object> contextData) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<Context, Task> action, Context context) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<Task> action, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<Task> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<Task> action, Context context, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<Context, Task> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<Context, Task> action, Context context, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<Context, CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAsync(action, new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken) { return ExecuteAsync(action, context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task ExecuteAsync(Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<Context, CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync(action, new Context(contextData), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task ExecuteAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) throw new ArgumentNullException("context"); SetPolicyContext(context); return ExecuteAsyncInternal(action, context, cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> action) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> action, IDictionary<string, object> contextData) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> action, Context context) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<Context, Task<TResult>> action, IDictionary<string, object> contextData) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<Context, Task<TResult>> action, Context context) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> action, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> action, Context context, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<Context, Task<TResult>> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<Context, Task<TResult>> action, Context context, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAsync(action, new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken) { return ExecuteAsync(action, context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAsync(action, new Context(contextData), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) throw new ArgumentNullException("context"); SetPolicyContext(context); return ExecuteAsyncInternal(action, context, cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Task> action) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Task> action, IDictionary<string, object> contextData) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Task> action, Context context) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, Task> action, IDictionary<string, object> contextData) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, Task> action, Context context) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Task> action, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Task> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Task> action, Context context, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, Task> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, Task> action, Context context, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync(action, new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync(action, new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync(action, context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync(action, new Context(contextData), cancellationToken, continueOnCapturedContext); } public async Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (_asyncExceptionPolicy == null) throw new InvalidOperationException("Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods."); if (context != null) try { await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); return PolicyResult.Successful(context); } catch (Exception exception) { return PolicyResult.Failure(exception, GetExceptionType(base.ExceptionPredicates, exception), context); } throw new ArgumentNullException("context"); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Task<TResult>> action) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Task<TResult>> action, IDictionary<string, object> contextData) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Task<TResult>> action, Context context) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, Task<TResult>> action, IDictionary<string, object> contextData) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, Task<TResult>> action, Context context) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Task<TResult>> action, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Task<TResult>> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Task<TResult>> action, Context context, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, Task<TResult>> action, IDictionary<string, object> contextData, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, Task<TResult>> action, Context context, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync(action, new Context(contextData), cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken) { return ExecuteAndCaptureAsync(action, context, cancellationToken, PolicyBase.DefaultContinueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken, continueOnCapturedContext); } [DebuggerStepThrough] public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, IDictionary<string, object> contextData, CancellationToken cancellationToken, bool continueOnCapturedContext) { return ExecuteAndCaptureAsync(action, new Context(contextData), cancellationToken, continueOnCapturedContext); } public async Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (_asyncExceptionPolicy == null) throw new InvalidOperationException("Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods."); if (context != null) try { return PolicyResult<TResult>.Successful(await this.ExecuteAsync<TResult>(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext), context); } catch (Exception exception) { return PolicyResult<TResult>.Failure(exception, GetExceptionType(base.ExceptionPredicates, exception), context); } throw new ArgumentNullException("context"); } protected Policy(Action<Action<Context, CancellationToken>, Context, CancellationToken> exceptionPolicy, IEnumerable<ExceptionPredicate> exceptionPredicates) { if (exceptionPolicy == null) throw new ArgumentNullException("exceptionPolicy"); _exceptionPolicy = exceptionPolicy; base.ExceptionPredicates = (exceptionPredicates ?? PredicateHelper.EmptyExceptionPredicates); } [DebuggerStepThrough] internal virtual void ExecuteInternal(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { if (_exceptionPolicy == null) throw new InvalidOperationException("Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods."); _exceptionPolicy(action, context, cancellationToken); } [DebuggerStepThrough] internal virtual TResult ExecuteInternal<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) { if (_exceptionPolicy == null) throw new InvalidOperationException("Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods."); TResult result = (TResult)default(TResult); _exceptionPolicy(delegate(Context ctx, CancellationToken ct) { result = (TResult)action(ctx, ct); }, context, cancellationToken); return (TResult)result; } [DebuggerStepThrough] public void Execute(Action action) { Execute((Action<Context, CancellationToken>)delegate { action(); }, new Context(), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public void Execute(Action action, IDictionary<string, object> contextData) { Execute((Action<Context, CancellationToken>)delegate { action(); }, new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public void Execute(Action action, Context context) { Execute((Action<Context, CancellationToken>)delegate { action(); }, context, PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public void Execute(Action<Context> action, IDictionary<string, object> contextData) { Execute(delegate(Context ctx, CancellationToken ct) { action(ctx); }, new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public void Execute(Action<Context> action, Context context) { Execute(delegate(Context ctx, CancellationToken ct) { action(ctx); }, context, PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public void Execute(Action<CancellationToken> action, CancellationToken cancellationToken) { Execute(delegate(Context ctx, CancellationToken ct) { action(ct); }, new Context(), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public void Execute(Action<CancellationToken> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { Execute(delegate(Context ctx, CancellationToken ct) { action(ct); }, new Context(contextData), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public void Execute(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) { Execute(delegate(Context ctx, CancellationToken ct) { action(ct); }, context, cancellationToken); } [DebuggerStepThrough] public void Execute(Action<Context, CancellationToken> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { Execute(action, new Context(contextData), cancellationToken); } [DebuggerStepThrough] public void Execute(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { if (context == null) throw new ArgumentNullException("context"); SetPolicyContext(context); ExecuteInternal(action, context, cancellationToken); } [DebuggerStepThrough] public TResult Execute<TResult>(Func<TResult> action) { return Execute((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public TResult Execute<TResult>(Func<TResult> action, IDictionary<string, object> contextData) { return Execute((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public TResult Execute<TResult>(Func<TResult> action, Context context) { return Execute((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public TResult Execute<TResult>(Func<Context, TResult> action, IDictionary<string, object> contextData) { return Execute((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public TResult Execute<TResult>(Func<Context, TResult> action, Context context) { return Execute((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public TResult Execute<TResult>(Func<CancellationToken, TResult> action, CancellationToken cancellationToken) { return Execute((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public TResult Execute<TResult>(Func<CancellationToken, TResult> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return Execute((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public TResult Execute<TResult>(Func<CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) { return Execute((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken); } [DebuggerStepThrough] public TResult Execute<TResult>(Func<Context, CancellationToken, TResult> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return Execute(action, new Context(contextData), cancellationToken); } [DebuggerStepThrough] public TResult Execute<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) { if (context == null) throw new ArgumentNullException("context"); SetPolicyContext(context); return ExecuteInternal(action, context, cancellationToken); } [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action action) { return ExecuteAndCapture((Action<Context, CancellationToken>)delegate { action(); }, new Context(), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult ExecuteAndCapture(Action action, IDictionary<string, object> contextData) { return ExecuteAndCapture((Action<Context, CancellationToken>)delegate { action(); }, new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult ExecuteAndCapture(Action action, Context context) { return ExecuteAndCapture((Action<Context, CancellationToken>)delegate { action(); }, context, PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action<Context> action, IDictionary<string, object> contextData) { return ExecuteAndCapture(delegate(Context ctx, CancellationToken ct) { action(ctx); }, new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action<Context> action, Context context) { return ExecuteAndCapture(delegate(Context ctx, CancellationToken ct) { action(ctx); }, context, PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action<CancellationToken> action, CancellationToken cancellationToken) { return ExecuteAndCapture(delegate(Context ctx, CancellationToken ct) { action(ct); }, new Context(), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult ExecuteAndCapture(Action<CancellationToken> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCapture(delegate(Context ctx, CancellationToken ct) { action(ct); }, new Context(contextData), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult ExecuteAndCapture(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) { return ExecuteAndCapture(delegate(Context ctx, CancellationToken ct) { action(ct); }, context, cancellationToken); } [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action<Context, CancellationToken> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCapture(action, new Context(contextData), cancellationToken); } [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { if (_exceptionPolicy == null) throw new InvalidOperationException("Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods."); if (context != null) try { Execute(action, context, cancellationToken); return PolicyResult.Successful(context); } catch (Exception exception) { return PolicyResult.Failure(exception, GetExceptionType(base.ExceptionPredicates, exception), context); } throw new ArgumentNullException("context"); } [DebuggerStepThrough] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<TResult> action) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(), new Context(), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<TResult> action, IDictionary<string, object> contextData) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(), new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<TResult> action, Context context) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(), context, PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<Context, TResult> action, IDictionary<string, object> contextData) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(ctx), new Context(contextData), PolicyBase.DefaultCancellationToken); } [DebuggerStepThrough] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<Context, TResult> action, Context context) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(ctx), context, PolicyBase.DefaultCancellationToken); } public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<CancellationToken, TResult> action, CancellationToken cancellationToken) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(ct), new Context(), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<CancellationToken, TResult> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(ct), new Context(contextData), cancellationToken); } [DebuggerStepThrough] [Obsolete("This overload is deprecated and scheduled for removal in Polly v6.")] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) { return ExecuteAndCapture((Context ctx, CancellationToken ct) => action(ct), context, cancellationToken); } [DebuggerStepThrough] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<Context, CancellationToken, TResult> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) { return ExecuteAndCapture(action, new Context(contextData), cancellationToken); } [DebuggerStepThrough] public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) { if (_exceptionPolicy == null) throw new InvalidOperationException("Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods."); if (context != null) try { return PolicyResult<TResult>.Successful(Execute(action, context, cancellationToken), context); } catch (Exception exception) { return PolicyResult<TResult>.Failure(exception, GetExceptionType(base.ExceptionPredicates, exception), context); } throw new ArgumentNullException("context"); } internal static ExceptionType GetExceptionType(IEnumerable<ExceptionPredicate> exceptionPredicates, Exception exception) { if (!exceptionPredicates.Any((ExceptionPredicate predicate) => predicate(exception) != null)) return ExceptionType.Unhandled; return ExceptionType.HandledByThisPolicy; } public static PolicyBuilder Handle<TException>() where TException : Exception { return new PolicyBuilder(delegate(Exception exception) { if (!(exception is TException)) return null; return exception; }); } public static PolicyBuilder Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception { return new PolicyBuilder(delegate(Exception exception) { TException arg; if ((arg = (exception as TException)) == null || !exceptionPredicate(arg)) return null; return exception; }); } public static PolicyBuilder HandleInner<TException>() where TException : Exception { return new PolicyBuilder(PolicyBuilder.HandleInner((Exception ex) => ex is TException)); } public static PolicyBuilder HandleInner<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception { return new PolicyBuilder(PolicyBuilder.HandleInner(delegate(Exception ex) { TException arg; if ((arg = (ex as TException)) != null) return exceptionPredicate(arg); return false; })); } public static PolicyBuilder<TResult> HandleResult<TResult>(Func<TResult, bool> resultPredicate) { return new PolicyBuilder<TResult>(resultPredicate); } public static PolicyBuilder<TResult> HandleResult<TResult>(TResult result) { return HandleResult(delegate(TResult r) { if (r == null || !r.Equals(result)) { if (r == null) return result == null; return false; } return true; }); } public Policy WithPolicyKey(string policyKey) { if (policyKeyInternal != null) throw PolicyBase.PolicyKeyMustBeImmutableException; policyKeyInternal = policyKey; return this; } ISyncPolicy ISyncPolicy.WithPolicyKey(string policyKey) { if (policyKeyInternal != null) throw PolicyBase.PolicyKeyMustBeImmutableException; policyKeyInternal = policyKey; return this; } IAsyncPolicy IAsyncPolicy.WithPolicyKey(string policyKey) { if (policyKeyInternal != null) throw PolicyBase.PolicyKeyMustBeImmutableException; policyKeyInternal = policyKey; return this; } protected Policy(Func<Func<Context, CancellationToken, Task>, Context, CancellationToken, bool, Task> asyncExceptionPolicy, IEnumerable<ExceptionPredicate> exceptionPredicates) { if (asyncExceptionPolicy == null) throw new ArgumentNullException("asyncExceptionPolicy"); _asyncExceptionPolicy = asyncExceptionPolicy; base.ExceptionPredicates = (exceptionPredicates ?? PredicateHelper.EmptyExceptionPredicates); } internal async Task ExecuteAsyncInternal(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (_asyncExceptionPolicy == null) throw new InvalidOperationException("Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods."); await _asyncExceptionPolicy(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); } internal virtual async Task<TResult> ExecuteAsyncInternal<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (_asyncExceptionPolicy == null) throw new InvalidOperationException("Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods."); TResult result = (TResult)default(TResult); await _asyncExceptionPolicy(async delegate(Context ctx, CancellationToken ct) { result = (TResult)(await action(ctx, ct).ConfigureAwait(continueOnCapturedContext)); }, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); return (TResult)result; } public static TimeoutPolicy Timeout(int seconds) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout(onTimeout: delegate { }, timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout(onTimeout: delegate { }, timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy Timeout(int seconds, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout((Context ctx) => TimeSpan.FromSeconds((double)seconds), TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout((Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy, onTimeout); } public static TimeoutPolicy Timeout(TimeSpan timeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeout, timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeout, timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy Timeout(TimeSpan timeout, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout((Context ctx) => timeout, TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout((Context ctx) => timeout, timeoutStrategy, onTimeout); } public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider, Action<Context, TimeSpan, Task> onTimeout) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout((Context ctx) => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout((Context ctx) => timeoutProvider(), timeoutStrategy, onTimeout); } public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider) { Action<Context, TimeSpan, Task> onTimeout = delegate { }; return Timeout(timeoutProvider, TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { Action<Context, TimeSpan, Task> onTimeout = delegate { }; return Timeout(timeoutProvider, timeoutStrategy, onTimeout); } public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider, Action<Context, TimeSpan, Task> onTimeout) { return Timeout(timeoutProvider, TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); if (onTimeout == null) throw new ArgumentNullException("onTimeout"); return new TimeoutPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { TimeoutEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, timeoutProvider, timeoutStrategy, onTimeout); }); } public static TimeoutPolicy TimeoutAsync(int seconds) { TimeoutValidator.ValidateSecondsTimeout(seconds); return TimeoutAsync(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask, timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateSecondsTimeout(seconds); return TimeoutAsync(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask, timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy TimeoutAsync(int seconds, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateSecondsTimeout(seconds); if (onTimeoutAsync == null) throw new ArgumentNullException("onTimeoutAsync"); return TimeoutAsync((Context ctx) => TimeSpan.FromSeconds((double)seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateSecondsTimeout(seconds); return TimeoutAsync((Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(TimeSpan timeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return TimeoutAsync(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask, timeoutProvider: (Context ctx) => timeout, timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return TimeoutAsync(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask, timeoutProvider: (Context ctx) => timeout, timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy TimeoutAsync(TimeSpan timeout, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return TimeoutAsync((Context ctx) => timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return TimeoutAsync((Context ctx) => timeout, timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(Func<TimeSpan> timeoutProvider) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask, timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy TimeoutAsync(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask, timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy TimeoutAsync(Func<TimeSpan> timeoutProvider, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync((Context ctx) => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync((Context ctx) => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(Func<Context, TimeSpan> timeoutProvider) { Func<Context, TimeSpan, Task, Task> onTimeoutAsync = (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask; return TimeoutAsync(timeoutProvider, TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { Func<Context, TimeSpan, Task, Task> onTimeoutAsync = (Context _, TimeSpan __, Task ___) => TaskHelper.EmptyTask; return TimeoutAsync(timeoutProvider, timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(Func<Context, TimeSpan> timeoutProvider, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { return TimeoutAsync(timeoutProvider, TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy TimeoutAsync(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); if (onTimeoutAsync == null) throw new ArgumentNullException("onTimeoutAsync"); return new TimeoutPolicy((Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => TimeoutEngine.ImplementationAsync(async delegate(Context ctx, CancellationToken ct) { await action(ctx, ct).ConfigureAwait(continueOnCapturedContext); return EmptyStruct.Instance; }, context, timeoutProvider, timeoutStrategy, onTimeoutAsync, cancellationToken, continueOnCapturedContext)); } public static TimeoutPolicy<TResult> Timeout<TResult>(int seconds) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout<TResult>(onTimeout: delegate { }, timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy<TResult> Timeout<TResult>(int seconds, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout<TResult>(onTimeout: delegate { }, timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy<TResult> Timeout<TResult>(int seconds, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout<TResult>((Context ctx) => TimeSpan.FromSeconds((double)seconds), TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(int seconds, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateSecondsTimeout(seconds); return Timeout<TResult>((Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(TimeSpan timeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout<TResult>(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeout, timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy<TResult> Timeout<TResult>(TimeSpan timeout, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout<TResult>(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeout, timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy<TResult> Timeout<TResult>(TimeSpan timeout, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout<TResult>((Context ctx) => timeout, TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return Timeout<TResult>((Context ctx) => timeout, timeoutStrategy, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<TimeSpan> timeoutProvider) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout<TResult>(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout<TResult>(onTimeout: delegate { }, timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<TimeSpan> timeoutProvider, Action<Context, TimeSpan, Task> onTimeout) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout<TResult>((Context ctx) => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return Timeout<TResult>((Context ctx) => timeoutProvider(), timeoutStrategy, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<Context, TimeSpan> timeoutProvider) { Action<Context, TimeSpan, Task> onTimeout = delegate { }; return Timeout<TResult>(timeoutProvider, TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { Action<Context, TimeSpan, Task> onTimeout = delegate { }; return Timeout<TResult>(timeoutProvider, timeoutStrategy, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<Context, TimeSpan> timeoutProvider, Action<Context, TimeSpan, Task> onTimeout) { return Timeout<TResult>(timeoutProvider, TimeoutStrategy.Optimistic, onTimeout); } public static TimeoutPolicy<TResult> Timeout<TResult>(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); if (onTimeout == null) throw new ArgumentNullException("onTimeout"); return new TimeoutPolicy<TResult>((Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult>)((Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) => TimeoutEngine.Implementation<TResult>(action, context, cancellationToken, timeoutProvider, timeoutStrategy, onTimeout))); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(int seconds) { TimeoutValidator.ValidateSecondsTimeout(seconds); return TimeoutAsync<TResult>(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)), timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(int seconds, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateSecondsTimeout(seconds); return TimeoutAsync<TResult>(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)), timeoutProvider: (Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(int seconds, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateSecondsTimeout(seconds); return TimeoutAsync<TResult>((Context ctx) => TimeSpan.FromSeconds((double)seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(int seconds, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateSecondsTimeout(seconds); return TimeoutAsync<TResult>((Context ctx) => TimeSpan.FromSeconds((double)seconds), timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(TimeSpan timeout) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return TimeoutAsync<TResult>(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)), timeoutProvider: (Context ctx) => timeout, timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(TimeSpan timeout, TimeoutStrategy timeoutStrategy) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return TimeoutAsync<TResult>(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)), timeoutProvider: (Context ctx) => timeout, timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(TimeSpan timeout, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); if (onTimeoutAsync == null) throw new ArgumentNullException("onTimeoutAsync"); return TimeoutAsync<TResult>((Context ctx) => timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); return TimeoutAsync<TResult>((Context ctx) => timeout, timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<TimeSpan> timeoutProvider) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync<TResult>(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)), timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: TimeoutStrategy.Optimistic); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync<TResult>(onTimeoutAsync: (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)), timeoutProvider: (Context ctx) => timeoutProvider(), timeoutStrategy: timeoutStrategy); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<TimeSpan> timeoutProvider, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync<TResult>((Context ctx) => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); return TimeoutAsync<TResult>((Context ctx) => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<Context, TimeSpan> timeoutProvider) { Func<Context, TimeSpan, Task, Task> onTimeoutAsync = (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)); return TimeoutAsync<TResult>(timeoutProvider, TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy) { Func<Context, TimeSpan, Task, Task> onTimeoutAsync = (Context _, TimeSpan __, Task ___) => TaskHelper.FromResult<TResult>(default(TResult)); return TimeoutAsync<TResult>(timeoutProvider, timeoutStrategy, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<Context, TimeSpan> timeoutProvider, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { return TimeoutAsync<TResult>(timeoutProvider, TimeoutStrategy.Optimistic, onTimeoutAsync); } public static TimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Task> onTimeoutAsync) { if (timeoutProvider == null) throw new ArgumentNullException("timeoutProvider"); if (onTimeoutAsync == null) throw new ArgumentNullException("onTimeoutAsync"); return new TimeoutPolicy<TResult>((Func<Func<Context, CancellationToken, Task<TResult>>, Context, CancellationToken, bool, Task<TResult>>)((Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => TimeoutEngine.ImplementationAsync<TResult>(action, context, timeoutProvider, timeoutStrategy, onTimeoutAsync, cancellationToken, continueOnCapturedContext))); } public PolicyWrap Wrap(ISyncPolicy innerPolicy) { if (innerPolicy == null) throw new ArgumentNullException("innerPolicy"); return new PolicyWrap(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationtoken) { PolicyWrapEngine.Implementation(action, context, cancellationtoken, this, innerPolicy); }, this, innerPolicy); } public PolicyWrap<TResult> Wrap<TResult>(ISyncPolicy<TResult> innerPolicy) { if (innerPolicy == null) throw new ArgumentNullException("innerPolicy"); return new PolicyWrap<TResult>((Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult>)((Func<Context, CancellationToken, TResult> func, Context context, CancellationToken cancellationtoken) => PolicyWrapEngine.Implementation<TResult>(func, context, cancellationtoken, (ISyncPolicy)this, innerPolicy)), this, (IsPolicy)innerPolicy); } public static PolicyWrap Wrap(params ISyncPolicy[] policies) { int num = policies.Length; if ((uint)num <= 1) throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", "policies"); if (num == 2) return new PolicyWrap(delegate(Action<Context, CancellationToken> func, Context context, CancellationToken cancellationtoken) { PolicyWrapEngine.Implementation(func, context, cancellationtoken, policies[0], policies[1]); }, (Policy)policies[0], policies[1]); return Wrap(policies[0], Wrap(policies.Skip(1).ToArray())); } public static PolicyWrap<TResult> Wrap<TResult>(params ISyncPolicy<TResult>[] policies) { int num = policies.Length; if ((uint)num <= 1) throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", "policies"); if (num == 2) return new PolicyWrap<TResult>((Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult>)((Func<Context, CancellationToken, TResult> func, Context context, CancellationToken cancellationtoken) => PolicyWrapEngine.Implementation<TResult>(func, context, cancellationtoken, policies[0], policies[1])), (Policy<TResult>)policies[0], (IsPolicy)policies[1]); return Wrap<TResult>(policies[0], Wrap(policies.Skip(1).ToArray())); } public PolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { if (innerPolicy == null) throw new ArgumentNullException("innerPolicy"); return new PolicyWrap((Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationtoken, bool continueOnCapturedContext) => PolicyWrapEngine.ImplementationAsync(action, context, cancellationtoken, continueOnCapturedContext, this, innerPolicy), this, innerPolicy); } public PolicyWrap<TResult> WrapAsync<TResult>(IAsyncPolicy<TResult> innerPolicy) { if (innerPolicy == null) throw new ArgumentNullException("innerPolicy"); return new PolicyWrap<TResult>((Func<Func<Context, CancellationToken, Task<TResult>>, Context, CancellationToken, bool, Task<TResult>>)((Func<Context, CancellationToken, Task<TResult>> func, Context context, CancellationToken cancellationtoken, bool continueOnCapturedContext) => PolicyWrapEngine.ImplementationAsync<TResult>(func, context, cancellationtoken, continueOnCapturedContext, (IAsyncPolicy)this, innerPolicy)), this, (IsPolicy)innerPolicy); } public static PolicyWrap WrapAsync(params IAsyncPolicy[] policies) { int num = policies.Length; if ((uint)num <= 1) throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", "policies"); if (num == 2) return new PolicyWrap((Func<Context, CancellationToken, Task> func, Context context, CancellationToken cancellationtoken, bool continueOnCapturedContext) => PolicyWrapEngine.ImplementationAsync(func, context, cancellationtoken, continueOnCapturedContext, policies[0], policies[1]), (Policy)policies[0], policies[1]); return WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())); } public static PolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] policies) { int num = policies.Length; if ((uint)num <= 1) throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", "policies"); if (num == 2) return new PolicyWrap<TResult>((Func<Func<Context, CancellationToken, Task<TResult>>, Context, CancellationToken, bool, Task<TResult>>)((Func<Context, CancellationToken, Task<TResult>> func, Context context, CancellationToken cancellationtoken, bool continueOnCapturedContext) => PolicyWrapEngine.ImplementationAsync<TResult>(func, context, cancellationtoken, continueOnCapturedContext, policies[0], policies[1])), (Policy<TResult>)policies[0], (IsPolicy)policies[1]); return WrapAsync<TResult>(policies[0], WrapAsync(policies.Skip(1).ToArray())); } } }