什么是Microsoft.Practices.TransientFaultHandling.RetryPolicy的正确代码?
我使用以下代码:什么是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)
请让我知道如果这个代码将重试,感谢
当你设置一个重试策略,您可以指定将控制退役类,你的情况是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;
}
}
感谢您的方式来跟踪重试。但仍然,什么是正确的代码重试?为什么短暂的错误是不值得重试,我认为只有永久性的错误,如错误的帐户等不值得重试 – 2012-03-29 12:13:18
我想它不认为错误'不能连接,因为目标机器积极拒绝它70.37.127.112:443'一个瞬间的错误,即应该每隔一段时间发生一次。 – 2012-03-29 15:03:21
我在反射器(http://www.reflector.net/)中反编译'StorageTransientErrorDetectionStrategy'类,看看它是如何确定哪些错误是暂时的。 – 2012-03-29 15:04:19
顺便说一句,我在MS一个古老的线程这也http://social.msdn.microsoft.com/Forums/en-US/windowsazuretroubleshooting/thread/b55d0126-f80b-4df2-bcb6-8b5da37c51ff – 2012-03-28 09:27:25