2012-03-28 72 views
1

我使用以下代碼:什麼是Microsoft.Practices.TransientFaultHandling.RetryPolicy的正確代碼?

創建一個重試策略,當出現錯誤時,在1秒後重試,然後等待3秒,然後等待5秒鐘。基於Azure的SLA,重試10秒必須成功之內(不節流,我確信這一點。因爲錯誤甚至發生在獨特的分區表,沒有交通還)

var retryStrategy = new Incremental(3, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)); 
var FaultHandlingRetryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy); 

然後我用這個代碼來獲取數據

FaultHandlingRetryPolicy .ExecuteAction(() => 
     { 
      var results = (from q in Query select new q).ToList(); 
     }); 

我不,如果它的重試還是不行,知道是因爲錯誤日誌未顯示

錯誤:

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 70.37.127.112:443 
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) 
    --- End of inner exception stack trace --- 
    at System.Net.HttpWebRequest.GetResponse() 
    at System.Data.Services.Client.QueryResult.Execute() 
    at System.Data.Services.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents) 
    at System.Data.Services.Client.DataServiceQuery`1.Execute() 
    at System.Data.Services.Client.DataServiceQuery`1.GetEnumerator() 
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
    at Microsoft.Practices.TransientFaultHandling.RetryPolicy.<>c__DisplayClass1.<ExecuteAction>b__0() 
    at Microsoft.Practices.TransientFaultHandling.RetryPolicy.ExecuteAction[TResult](Func`1 func) 

請讓我知道如果這個代碼將重試,感謝

+0

順便說一句,我在MS一個古老的線程這也http://social.msdn.microsoft.com/Forums/en-US/windowsazuretroubleshooting/thread/b55d0126-f80b-4df2-bcb6-8b5da37c51ff – 2012-03-28 09:27:25

回答

5

當你設置一個重試策略,您可以指定將控制退役類,你的情況是StorageTransientErrorDetectionStrategy

每次在ExecuteAction()方法中引發異常時,該類將決定是否可以執行重試。例如一些例外是暫時的,所以不值得重試它們。

要實際跟蹤的重試次數,你可以使用這樣的代碼:

FaultHandlingRetryPolicy.Retrying += (obj, eventArgs) => 
      { 
       Console.Writeline("Retrying, CurrentRetryCount = {0} , Exception = {1}", eventArgs.CurrentRetryCount, eventArgs.LastException.Message); 
      }; 

更新

你可以像這樣創建自己的錯誤處理策略和就地使用它標準的一個。 但是你必須調整錯誤以適應你的情況,這些對我來說很合適。

public class CustomSqlAzureTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy 
    { 
     private readonly SqlAzureTransientErrorDetectionStrategy _normalStrategy = 
      new SqlAzureTransientErrorDetectionStrategy(); 

     public bool IsTransient(Exception ex) 
     { 
      if (_normalStrategy.IsTransient(ex)) 
       return true; 

      //do our custom logic 
      if (ex is SqlException) 
      { 
       var sqEx = ex as SqlException; 
       if (sqEx.Message.Contains("The timeout period elapsed prior to completion of the operation or the server is not responding") || 
        sqEx.Message.Contains("An existing connection was forcibly closed by the remote host") || 
        sqEx.Message.Contains("The service has encountered an error processing your request. Please try again") || 
        sqEx.Message.Contains("Timeout expired") || 
        sqEx.Message.Contains("was deadlocked on lock resources with another process and has been chosen as the deadlock victim") || 
        sqEx.Message.Contains("A transport-level error has occurred when receiving results from the server")) 
       {       
        return true; 
       } 
      } 

      return false; 
     } 
    } 
+0

感謝您的方式來跟蹤重試。但仍然,什麼是正確的代碼重試?爲什麼短暫的錯誤是不值得重試,我認爲只有永久性的錯誤,如錯誤的帳戶等不值得重試 – 2012-03-29 12:13:18

+0

我想它不認爲錯誤'不能連接,因爲目標機器積極拒絕它70.37.127.112:443'一個瞬間的錯誤,即應該每隔一段時間發生一次。 – 2012-03-29 15:03:21

+1

我在反射器(http://www.reflector.net/)中反編譯'StorageTransientErrorDetectionStrategy'類,看看它是如何確定哪些錯誤是暫時的。 – 2012-03-29 15:04:19

相關問題