Quantcast
Channel: Active questions tagged retry-logic - Stack Overflow
Viewing all articles
Browse latest Browse all 950

Polly doesn't work if function wraps up with try catch [closed]

$
0
0

I am using Polly for SQL exception in my code.

I try to catch errors from SqlConnection and SqlCommand executes. In this situation, code executes the catch block directly, but never retries to connect to SQL Server.

RetryAutoPolicy retryAutoPolicy = new RetryAutoPolicy();retryAutoPolicy.retryEvery5SecondsUpTo3Minutes.Execute(() =>      {          using (var conn = new SqlConnection(ProjDB_ConnectionString))          {              try              {                   // This part is not handling Retrying.. Directly  went to catch block. Instead of retry for 3 minutes for every 5 seconds                 conn.Open();                       string selectCommand = "SELECT TOP 1 * FROM [dbo].[TABLEA]";                 var command = new SqlCommand(selectCommand, conn);                 // Console.WriteLine($" commandTime out is {command.CommandTimeout} And Connection Timeout is {conn.ConnectionTimeout}");                 using (SqlDataReader reader = command.ExecuteReader())                 {                      while (reader.Read())                      {                          Console.WriteLine($"Response is - " + String.Format("{0}", reader[0]));                      }                 }              }              catch (Exception e)              {                  Console.WriteLine($"Erros is" + e.Message);              }              conn.Close();          }      });public Policy retryEvery5SecondsUpTo3Minutes{    Policy timeoutAfter3Minutes = Policy.Timeout(TimeSpan.FromMinutes(totalRetryTimeLimit));    Policy retrySqlConnEvery5Seconds = Policy.Handle<SqlException>                                     .WaitAndRetryForever(sleepDurationProvider: i => TimeSpan.FromSeconds(delaySecondsAfterTries)                                     , (exception, retry, timespan) =>                                     {                                        Console.WriteLine($"Retry Count [{retry}] ");                                     });    retryEvery5SecondsUpTo3Minutes = timeoutAfter3Minutes.Wrap(retrySqlConnEvery5Seconds);}

I already have try catch before SqlConnection is opened, and execute command which I can not change for our application.

I added retry to handle if SQL Server is down.


Viewing all articles
Browse latest Browse all 950

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>