今日,对代码进行单元测试时,发现方法GetAllSupplyTypes报如下错误:
[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.
经过对比,唯一的差异是该方法返回的数据对象是一个继承于另一个集合对象的对象,代码如下:
/// <summary>
/// SupplyType的值的集合
/// </summary>
[DataContract]
public class SupplyTypeResults : List<SupplyType>
{
/// <summary>
/// 构造函数
/// </summary>
public SupplyTypeResults()
{
this.Add(new SupplyType
{
SupplyTypeId = 2,
SupplyTypeName = "其他投放方式"
});
this.Add(new SupplyType
{
SupplyTypeId = 1,
SupplyTypeName = "平台自动投放"
});
this.Add(new SupplyType
{
SupplyTypeId = 0,
SupplyTypeName = "客户主动领取"
});
}
}
其中,SupplyType定义如下:
/// <summary>
/// 优惠券发行方式
/// </summary>
[DataContract]
public class SupplyType
{
/// <summary>
/// 发行方式Id
/// </summary>
[DataMember(Name = "supplyTypeId")]
public int SupplyTypeId { get; set; }
/// <summary>
/// 发行方式名称
/// </summary>
[DataMember(Name = "supplyTypeName")]
public string SupplyTypeName { get; set; }
}
自以为继承与一个WCF能正常序列化的对象,而且在SupplyTypeResults上也加了DataContract应该没有问题,但是就是报错。
后来不再使用SupplyTypeResults类,在代码中直接使用List<SupplyType>,问题全部解决。
之后,有发现另一个方法也会产生该问题,但是,该方法不属于此原因范畴,经过一阵排查,发现返回类中有个日期属性没有赋值,导致了该错误的抛出。
经过上述两事件总结可以得出,对于任何在序列化过程中可能出错的情况,都会导致服务器返回0字节数据而报此错误。