基于链接,您刚刚发布,代码样本提供:
// Define your retry strategy: retry 3 times, 1 second apart.
var retryStrategy = new FixedInterval(3, TimeSpan.FromSeconds(1));
// Define your retry policy using the retry strategy and the Azure storage
// transient fault detection strategy.
var retryPolicy =
new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy);
// Do some work that may result in a transient fault.
try
{
// Call a method that uses Azure storage and which may
// throw a transient exception.
retryPolicy.ExecuteAction(
() =>
{
this.queue.CreateIfNotExist();
});
}
catch (Exception)
{
// All of the retries failed.
}
根据该文件的例外则不会引发直到它不再是一个暂时的错误所界定的重试策略。实际上 - 利用瞬态故障处理应用程序块已经在做你在问你的问题了。重试会无提示地重试(无一例外),直到出现异常的时间点 - 发生重试超过重试策略时。
以下不应被视为“好码”,它只不过是在TFHAB 可能如何确定传输错误的例子。
private void DoStuff()
{
try
{
this.DoSomethingThatCouldPotentiallyCauseTransientErrors(5);
}
catch (Exception ex)
{
// This would not be caught until the "retries" have occurred.
this.HandleException(ex);
return RedirectToAction("Index", "Error");
}
}
private void DoSomethingThatCouldPotentiallyCauseTransientErrors(int retryAttemptsBeforeExceptionThrown)
{
// Note that this will *always throw an exception*,
// I'm (attempting to) simply demonstrate my point of how the transient errors could be defined.
for (int i = 0; i < retryAttemptsBeforeExceptionThrown)
{
try
{
int x = 0;
int y = 0;
int result = x/y;
}
catch (Exception)
{
if (i < retryAttemptsBeforeExceptionThrown-1)
{
// Swallow/ignore the exception, and retry
// Note that anything hitting this block would be considered a "transient error",
// as we are not raising an exception
}
else
{
// Too many failed attempts have occurred, ***now*** we raise an exception to the caller
throw;
}
}
}
}
private void HandleException(Exception ex)
{
// Implementation
}
希望诸如此类的回答约翰的问题上是什么让一个“暂时性错误”
你能发布你的代码? – 2015-02-11 17:04:30
以及您如何确定瞬态错误是什么? – Kritner 2015-02-11 17:04:40
写在MSDN https://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx瞬态错误是:瞬态故障是由于某些临时状态,例如网络中发生的错误连接问题或服务不可用性 – user4092086 2015-02-11 18:16:08