Rsft.EntLib.TransientFaultHandling.Standard by Rolosoft Ltd

<PackageReference Include="Rsft.EntLib.TransientFaultHandling.Standard" Version="7.0.0" />

 RetryManager

public class RetryManager
Provides the entry point to the retry functionality.
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Properties; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling { public class RetryManager { private static RetryManager defaultRetryManager; private IDictionary<string, RetryStrategy> retryStrategies; private string defaultRetryStrategyName; private IDictionary<string, string> defaultRetryStrategyNamesMap; private IDictionary<string, RetryStrategy> defaultRetryStrategiesMap; private RetryStrategy defaultStrategy; public static RetryManager Instance { get { RetryManager retryManager = defaultRetryManager; if (retryManager == null) throw new InvalidOperationException(Resources.ExceptionRetryManagerNotSet); return retryManager; } } public string DefaultRetryStrategyName { get { return defaultRetryStrategyName; } set { if (!string.IsNullOrWhiteSpace(value)) { if (!retryStrategies.TryGetValue(value, out RetryStrategy value2)) throw new ArgumentOutOfRangeException("value", string.Format(CultureInfo.CurrentCulture, Resources.RetryStrategyNotFound, value)); defaultRetryStrategyName = value; defaultStrategy = value2; } else defaultRetryStrategyName = null; } } public static void SetDefault(RetryManager retryManager, bool throwIfSet = true) { if (((defaultRetryManager != null) & throwIfSet) && retryManager != defaultRetryManager) throw new InvalidOperationException(Resources.ExceptionRetryManagerAlreadySet); defaultRetryManager = retryManager; } public RetryManager(IEnumerable<RetryStrategy> retryStrategies) : this(retryStrategies, null, null) { } public RetryManager(IEnumerable<RetryStrategy> retryStrategies, string defaultRetryStrategyName) : this(retryStrategies, defaultRetryStrategyName, null) { } public RetryManager(IEnumerable<RetryStrategy> retryStrategies, string defaultRetryStrategyName, IDictionary<string, string> defaultRetryStrategyNamesMap) { this.retryStrategies = retryStrategies.ToDictionary((RetryStrategy p) => p.Name); this.defaultRetryStrategyNamesMap = defaultRetryStrategyNamesMap; DefaultRetryStrategyName = defaultRetryStrategyName; defaultRetryStrategiesMap = new Dictionary<string, RetryStrategy>(); if (this.defaultRetryStrategyNamesMap != null) { foreach (KeyValuePair<string, string> item in from x in this.defaultRetryStrategyNamesMap where !string.IsNullOrWhiteSpace(x.Value) select x) { if (!this.retryStrategies.TryGetValue(item.Value, out RetryStrategy value)) throw new ArgumentOutOfRangeException("defaultRetryStrategyNamesMap", string.Format(CultureInfo.CurrentCulture, Resources.DefaultRetryStrategyMappingNotFound, item.Key, item.Value)); defaultRetryStrategiesMap.Add(item.Key, value); } } } public virtual RetryPolicy<T> GetRetryPolicy<T>() where T : ITransientErrorDetectionStrategy, new { return new RetryPolicy<T>(GetRetryStrategy()); } public virtual RetryPolicy<T> GetRetryPolicy<T>(string retryStrategyName) where T : ITransientErrorDetectionStrategy, new { return new RetryPolicy<T>(GetRetryStrategy(retryStrategyName)); } public virtual RetryStrategy GetRetryStrategy() { return defaultStrategy; } public virtual RetryStrategy GetRetryStrategy(string retryStrategyName) { Guard.ArgumentNotNullOrEmptyString(retryStrategyName, "retryStrategyName"); if (!retryStrategies.TryGetValue(retryStrategyName, out RetryStrategy value)) throw new ArgumentOutOfRangeException(string.Format(CultureInfo.CurrentCulture, Resources.RetryStrategyNotFound, retryStrategyName)); return value; } public virtual RetryStrategy GetDefaultRetryStrategy(string technology) { Guard.ArgumentNotNullOrEmptyString(technology, "techonology"); if (!defaultRetryStrategiesMap.TryGetValue(technology, out RetryStrategy value)) value = defaultStrategy; if (value == null) throw new ArgumentOutOfRangeException(string.Format(CultureInfo.CurrentCulture, Resources.DefaultRetryStrategyNotFound, technology)); return value; } } }