2012-04-10 61 views
1

我試圖使用Microsoft.WindowsAzure.StorageClient.RetryPolicy;無需連接到Azure服務P&P RetryPolicy,什麼是瞬態異常

var _retry = RetryPolicyFactory.GetRetryPolicy<StorageTransientErrorDetectionStrategy>("Incremental Retry Strategy"); 
    var result = _retry.ExecuteAction(()=> InnerRequest(data)); 

問題是 - 應該怎麼做InnerRequest方法做RetryPolicy開始工作? 它應該拋出某種指定的異常?通過在Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling組件(如StorageTransientErrorDetectionStrategy程式碼中)提供的錯誤檢測策略自動檢測

回答

5

瞬態錯誤,而這將觸發重試的政策。

實際上,這是基於您可以在中找到的Microsoft.Practices.TransientFaultHandling.Core程序集。在Azure的特定組件的每個錯誤檢測策略實現了以下接口:

/// <summary> 
/// Defines an interface which must be implemented by custom components responsible for detecting specific transient conditions. 
/// </summary> 
public interface ITransientErrorDetectionStrategy 
{ 
    /// <summary> 
    /// Determines whether the specified exception represents a transient failure that can be compensated by a retry. 
    /// </summary> 
    /// <param name="ex">The exception object to be verified.</param> 
    /// <returns>True if the specified exception is considered as transient, otherwise false.</returns> 
    bool IsTransient(Exception ex); 
} 

這裏是你使用StorageTransientErrorDetectionStrategy的例子:

WebException webException = ex as WebException; 
if (webException != null && (webException.Status == WebExceptionStatus.ProtocolError || webException.Status == WebExceptionStatus.ConnectionClosed)) 
{ 
    return true; 
} 
DataServiceRequestException dataServiceException = ex as DataServiceRequestException; 
if (dataServiceException != null && StorageTransientErrorDetectionStrategy.IsErrorStringMatch(StorageTransientErrorDetectionStrategy.GetErrorCode(dataServiceException), new string[] 
{ 
    "InternalError", 
    "ServerBusy", 
    "OperationTimedOut", 
    "TableServerOutOfMemory" 
})) 
{ 
    return true; 
} 
StorageServerException serverException = ex as StorageServerException; 
if (serverException != null) 
{ 
    if (StorageTransientErrorDetectionStrategy.IsErrorCodeMatch(serverException, new StorageErrorCode[] 
    { 
     1, 
     2 
    })) 
    { 
     return true; 
    } 
    if (StorageTransientErrorDetectionStrategy.IsErrorStringMatch(serverException, new string[] 
    { 
     "InternalError", 
     "ServerBusy", 
     "OperationTimedOut" 
    })) 
    { 
     return true; 
    } 
} 
StorageClientException storageException = ex as StorageClientException; 
return (storageException != null && StorageTransientErrorDetectionStrategy.IsErrorStringMatch(storageException, new string[] 
{ 
    "InternalError", 
    "ServerBusy", 
    "TableServerOutOfMemory" 
})) || ex is TimeoutException; 
相關問題