本文介绍了Polly断路器模式-用于测试连接线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Polly测试连接字符串是否为空.如果为空,我想使用CircuitBreaker尝试3次,并且该消息应在控制台"窗口中输出.

I'm trying to test whether the connection string is null using Polly. If it is null, I want to try three times using the CircuitBreaker and the message should be outputted in the Console window.

Policy policy = null;

// Break the circuit after the specified number of exceptions
// and keep circuit broken for the specified duration.
policy = Policy
               .Handle<NullReferenceException>()
               .CircuitBreaker(3, TimeSpan.FromSeconds(30));
try
   {
     string connected = policy.Execute(() => repository.GetConnectionString());
   }

catch (Exception ex)
      {
         Console.WriteLine("{0}",ex.Message);
      }

和GetConnectionString方法是:

and the GetConnectionString method is:

public string GetConnectionString()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["Test1"].ConnectionString;
        return conn.ConnectionString;
    }

为了测试这一点,我在App.config中更改了连接字符串名称.

In order to test this, in the App.config I have changed the connection string name.

但是,它似乎无法处理NullReference Exception.

However it doesn't seem to handle NullReference Exception.

当我调试应用程序时-它将打开未找到的CircuitBreakerEngine.cs并仅打印对象引用未设置为对象的实例".

When I debug the application - It opens CircuitBreakerEngine.cs not found and prints "Object reference not set to an instance of an object" only.

预期:要从断开的电路异常中打印未设置对象引用三次的对象引用

Expected :To print Object reference not set to an instance of an object thrice and teh message from the Broken circuit Exception

推荐答案

我相信您已经误解了CircuitBreaker策略的作用,如以下类似问题所述:

I believe you have misunderstood what the CircuitBreaker policy does, as described at this similar question: Polly framework CircuitBreakerAsync does not retry if exception occur

断路器本身不会安排任何重试.相反,它存在的目的是测量通过它执行的代表的故障率-如果故障率变得过高,则使电路跳闸.由于它的目的仅是作为测量和中断设备,它的确会抛出通过该设备执行的委托的异常:因此,您会看到NullReferenceException被重新抛出.

A circuit-breaker does not of itself orchestrate any retries. Rather, it exists to measure the rate of faults on delegates executed through it - and trip the circuit if the fault rate becomes too high. As its purpose is only as a measuring-and-breaking device, it indeed rethrows exceptions from delegates executed through it: hence the NullReferenceException you are seeing rethrown.

编辑:Polly Wiki上也清楚地描述了断路器的这种行为及其与重试的区别:"> https://github.com/App-vNext/Polly/wiki/Circuit-Breaker

EDIT: This behaviour of the circuit-breaker, and its difference from retry, is also clearly described in the Polly wiki, at: https://github.com/App-vNext/Polly/wiki/Circuit-Breaker

要做我想做的事,您需要将重试策略与断路器策略结合起来,如. Polly现在提供了 PolicyWrap ,以简化组合策略.

To do what I think you want to do, you need to combine retry policy with circuit breaker policy as described at Polly framework CircuitBreakerAsync does not retry if exception occur. Polly now offers PolicyWrap to make combining policies easy.

这篇关于Polly断路器模式-用于测试连接线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 17:37