/// <summary>
        /// FallbackPolicy是多线程安全的和可重用的
        /// </summary>
        /// <param name="func">要执行的业务逻辑</param>
        /// <param name="fallbackAction">业务逻辑执行失败后,执行的挽救代码</param>
        /// <returns></returns>
        public static PolicyResult <T> Fallback <T>(Func <T> func, Action fallbackAction = null)
        {
            FallbackPolicy policy = Policy.Handle <Exception>().Fallback(() =>
            {
                fallbackAction?.Invoke();
            });

            return(policy.ExecuteAndCapture(func));
        }
        //FallbackPolicy是多线程安全的和可重用的

        /// <summary>
        /// FallbackPolicy是多线程安全的和可重用的
        /// </summary>
        /// <param name="action">要执行的业务逻辑</param>
        /// <param name="fallbackAction">业务逻辑执行失败后,执行的挽救代码</param>
        /// <returns></returns>
        public static PolicyResult Fallback(Action action, Action fallbackAction = null)
        {
            FallbackPolicy policy = Policy.Handle <Exception>().Fallback(() =>
            {
                fallbackAction?.Invoke();
            });

            return(policy.ExecuteAndCapture(action));
        }
Exemplo n.º 3
0
        public void Should_call_fallbackAction_with_the_passed_context_when_execute_and_capture()
        {
            IDictionary <string, object> contextData = null;

            Func <Context, CancellationToken, ResultPrimitive> fallbackAction = (ctx, ct) => { contextData = ctx; return(ResultPrimitive.Substitute); };

            Action <DelegateResult <ResultPrimitive>, Context> onFallback = (ex, ctx) => { };

            FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy
                                                              .HandleResult(ResultPrimitive.Fault)
                                                              .Fallback(fallbackAction, onFallback);

            fallbackPolicy.ExecuteAndCapture(() => { return(ResultPrimitive.Fault); },
                                             new { key1 = "value1", key2 = "value2" }.AsDictionary())
            .Result.Should().Be(ResultPrimitive.Substitute);

            contextData.Should()
            .ContainKeys("key1", "key2").And
            .ContainValues("value1", "value2");
        }
Exemplo n.º 4
0
        public void Should_call_fallbackAction_with_the_fault_when_execute_and_capture()
        {
            DelegateResult <ResultPrimitive> fallbackOutcome = null;

            Func <DelegateResult <ResultPrimitive>, Context, CancellationToken, ResultPrimitive> fallbackAction =
                (outcome, ctx, ct) => { fallbackOutcome = outcome; return(ResultPrimitive.Substitute); };

            Action <DelegateResult <ResultPrimitive>, Context> onFallback = (ex, ctx) => { };

            FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy <ResultPrimitive>
                                                              .HandleResult(ResultPrimitive.Fault)
                                                              .Fallback(fallbackAction, onFallback);

            var result = fallbackPolicy.ExecuteAndCapture(() => { return(ResultPrimitive.Fault); });

            result.Should().NotBeNull();
            result.Result.Should().Be(ResultPrimitive.Substitute);

            fallbackOutcome.Should().NotBeNull();
            fallbackOutcome.Exception.Should().BeNull();
            fallbackOutcome.Result.Should().Be(ResultPrimitive.Fault);
        }