WCF問題集錦:ReadResponse failed: The server did not return a complete response for this request.


今日。對代碼進行單元測試時。發現方法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>。問題所有解決。


之后。有發現還有一個方法也會產生該問題。可是,該方法不屬於此原因范疇。經過一陣排查,發現返回類中有個日期屬性沒有賦值,導致了該錯誤的拋出。

經過上述兩事件總結能夠得出,對於不論什么在序列化過程中可能出錯的情況。都會導致server返回0字節數據而報此錯誤。









免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM