我正在使用 FtpWebRequest 和瞬态故障处理应用程序块。对于我的故障处理程序,我有一个错误检测策略来检查响应是否被认为是瞬态的,以便它知道是否重试:
public bool IsTransient(Exception ex)
{
var isTransient = false;
//will be false if the exception is not a web exception.
var webEx = ex as WebException;
//happens when receiving a protocol error.
//This protocol error wraps the inner exception, e.g. a 401 access denied.
if (webEx != null && webEx.Status == WebExceptionStatus.ProtocolError)
{
var response = webEx.Response as FtpWebResponse;
if (response != null && (int)response.StatusCode < 400)
{
isTransient = true;
}
}
// if it is a web exception but not a protocol error,
// check the status code.
else if (webEx != null)
{
//(check for transient error statuses here...)
isTransient = true;
}
return isTransient;
}
我正在尝试编写一些测试来检查是否将适当的错误标记为瞬态,但是我在创建或模拟具有 FtpWebResponse 内部异常的 Web 异常时遇到了麻烦(因此下面的响应不是t 始终为空)
var response = webEx.Response as FtpWebResponse;
有人知道我该怎么做吗?我会以正确的方式去做吗?