我试图使用Microsoft.WindowsAzure.StorageClient.RetryPolicy;无需连接到Azure服务

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


问题是-InnerRequest方法对RetryPolicy开始起作用应该做什么?
它应该抛出某种指定的异常?

最佳答案

临时错误由Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling程序集中的错误检测策略自动检测(如代码片段中的StorageTransientErrorDetectionStrategy),这将触发重试策略。

实际上,这基于您可以在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;

关于c# - P&P RetryPolicy,什么是瞬时异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10097612/

10-09 09:26