我正在使用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始终为null)
var response = webEx.Response as FtpWebResponse;
有人知道我该怎么做吗?我会以正确的方式进行操作吗?
使用适当的构造函数WebException
来设置响应:
public WebException(
string message,
Exception innerException,
WebExceptionStatus status,
WebResponse response)
使用FtpWebResponse设置异常是我遇到的麻烦... FtpWebResponse具有我无法访问的内部构造函数。
BCL并非真正用于测试,因为该概念在编写时并不大。您必须使用反射调用该内部构造函数(使用反编译器查看可用的内容)。或者,使用自定义可模拟类包装所需的所有System.Net类。不过,这似乎需要大量工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句