Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c# - Polly Circuit breaker pattern - For testing connection strings

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);               
      }     

and the GetConnectionString method is:

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

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

However it doesn't seem to handle NullReference Exception.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...