public static Forecast FallbackPolicyExample() { /* * This example will try to call the default service, if that fails it will call an alternate service that provides * the same information. */ var result = FallbackPolicy.GetFallbackPolicy(_fallbackWeatherService.GetForecast) .Execute(() => _weatherService.GetForecast(true)); return(result); }
public static Forecast CircuitBreakerExample(bool shouldFail = true) { /* * This example will trigger a circuit breaker. It will make WeatherService unavailable for a period of time after N failures. * As an additional example, we will combine this circuit breaker so all subsequent calls will fallback fo FallbackWeatherService * while the circuit is broken. */ var fallbackPolicy = FallbackPolicy.GetFallbackPolicy(_fallbackWeatherService.GetForecast); var policyWrap = fallbackPolicy.Wrap(CircuitBreakerPolicy.circuitBreakerPolicy); var result = policyWrap.Execute(() => _weatherService.GetForecast(shouldFail)); return(result); }
public static Forecast WrapPoliciesExample() { var retryPolicy = RetryPolicy.GetRetryPolicy(1); var retryAndWaitPolicy = RetryPolicy.GetRetryAndWaitPolicy(1); var fallbackPolicy = FallbackPolicy.GetFallbackPolicy(_fallbackWeatherService.GetForecast); // Policy Wrap can combine multiple policies. // In this example, try retrying immediately first, then wait N seconds to retry, and if that fails, use a fallback. // Policy run order is from the latest wrapped to the first wrapped. // This will execute retry, then retryAndWait and finally fallback. var policyWrap = fallbackPolicy.Wrap(retryAndWaitPolicy).Wrap(retryPolicy); var result = policyWrap.Execute(() => _weatherService.GetForecast(true)); return(result); }