Polly Circuit breaker pattern - For testing connection strings
<p>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.</p>
<pre><code>Policy policy = null;
   
// Break the circuit after the specified number of exceptions
// and keep circuit broken for the specified duration.
policy = Policy
               .Handle&lt;NullReferenceException&gt;()
               .CircuitBreaker(3, TimeSpan.FromSeconds(30)); 
try
{
    string connected = policy.Execute(() =&gt; repository.GetConnectionString());
}       
catch (Exception ex)
{
    Console.WriteLine(&quot;{0}&quot;,ex.Message);               
}     
</code></pre>
<p>and the <code>GetConnectionString</code> method is:</p>
<pre><code>public string GetConnectionString()
{
     SqlConnection conn = new SqlConnection();
     conn.ConnectionString = ConfigurationManager.ConnectionStrings[&quot;Test1&quot;].ConnectionString;
     return conn.ConnectionString;
}
</code></pre>
<p>In order to test this, in the App.config I have changed the connection string name.</p>
<pre class="lang-xml prettyprint-override"><code>&lt;connectionStrings&gt;
    &lt;add name=&quot;Testconnectionstring&quot; connectionString=&quot;Data Source=(Local);Initial Catalog=Test;Integrated Security=SSPI;&quot; providerName=&quot;System.Data.SqlClient&quot; /&gt;
&lt;/connectionStrings&gt;
</code></pre>
<p>However it doesn't seem to handle <code>NullReferenceException</code>.</p>
<p>When I debug the application - It opens CircuitBreakerEngine.cs not found and prints only</p>
<blockquote>
<p>&quot;Object reference not set to an instance of an object&quot;.</p>
</blockquote>
<p>Expected :
To print Object reference not set to an instance of an object thrice and teh message from the <code>BrokenCircuitException</code></p>