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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…