我試圖返回類的內部對象的列表,但是當我嘗試讀取容器類的結果時,它將列表中的空值刪除。我對Web API非常陌生,所以我不知道這是通過設計還是我做錯了什麼。如果我將響應內容讀入字符串,那麼列表就在那裏。第一張圖片顯示列表完整,第二張顯示他們不再可用。任何方向將不勝感激! Web API Restful服務ReadAsAsync <type>不返回列表
using (HttpClient client = new HttpClient())
{
string url = Url(String.Format("{0}", id));
SetupClient(client, url);
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
shipment = response.Content.ReadAsAsync<ShipmentViewModel>().Result;
}
編輯添加ShipmentViewModel源
public class ShipmentViewModel
{
public Guid Id { get; set; }
public String ShipmentName { get; set; }
public String Description { get; set; }
public String Comments { get; set; }
/// <summary>
/// Used for UI display purposes only
/// </summary>
public int NumberOrdersScanned { get; set; }
public int NumberOrdersTotal { get; set; }
public int NumberParcelsScanned { get; set; }
public int NumberParcelsTotal { get; set; }
public List<ShipmentEntity> ShipmentEntities { get; set; }
public ShipmentEntity ShipmentEntity { get; set; }
public Guid ValidationTypeId { get; set; }
// public string ValidationType { get; set; }
public ValidationType ValidationType { get; set; }
public List<ValidationType> ValidationTypes { get; set; }
/// <summary>
/// Used for UI display purposes only
/// </summary>
public List<ShipmentStatus> ShipmentStatuses { get; set; }
public ShipmentStatus ShipmentStatus { get; set; }
/// <summary>
/// Used for UI display purposes only
/// </summary>
public List<ShipmentDestinationCountry> DestinationCountries { get; set; }
public ShipmentDestinationCountry DestinationCountry { get; set; }
private List<ShipmentTrackingNumber> _TrackingNumbers;
public List<ShipmentTrackingNumber> TrackingNumbers
{
get
{
return _TrackingNumbers ?? (_TrackingNumbers = new List<ShipmentTrackingNumber>());
}
set
{
_TrackingNumbers = value;
}
}
public bool RequiresValidation { get; set; }
private List<ShipmentValidationViewModel> _Validations { get; set; }
public List<ShipmentValidationViewModel> Validations
{
get
{
return _Validations ?? (_Validations = new List<ShipmentValidationViewModel>());
}
set
{
_Validations = value;
}
}
public Guid EdiShipmentInfoId { get; set; }
public EdiShipmentInfoViewModel EdiShipmentInfo { get; set; }
public ShipmentEvents Events { get; set; }
}
這裏是EdiShipmentInfoViewModel
public class EdiShipmentInfoViewModel
{
// private string _packagingCode;
public Guid Id { get; set; }
public Guid ShipmentId { get; set; }
public Guid WarehouseId { get; set; }
public virtual InternalLookupModel Warehouse { get; set; }
public DateTime? ShippingDate { get; set; }
public DateTime? DeliveryDate { get; set; }
public string EquipmentNumber { get; set; }
public string ShippingWeight { get; set; }
public string PackagingCode { get; set; }
public string CarrierCommonName { get; set; }
public Guid ScacCodeId { get; set; }
public virtual InternalLookupModel ScacCode { get; set; }
public Guid PackagingFormId { get; set; }
public virtual InternalLookupModel PackagingForm { get; set; }
public Guid PackagingMaterialId { get; set; }
public virtual InternalLookupModel PackagingMaterial { get; set; }
public string MasterBillLading { get; set; }
public string BillLading { get; set; }
public string LadingDescription { get; set; }
public string FobShippingPoint { get; set; }
public string FobDestination { get; set; }
public Guid TransPayMethodId { get; set; }
public virtual InternalLookupModel TransPayMethod { get; set; }
public Guid TransTypeId { get; set; }
public virtual InternalLookupModel TransType { get; set; }
public Guid ServiceLevelCodeId { get; set; }
public virtual InternalLookupModel ServiceLevelCode { get; set; }
public Guid UnitOfMeasureId { get; set; }
public virtual InternalLookupModel UnitOfMeasure { get; set; }
private List<InternalLookupModel> _WarehouseList;
public List<InternalLookupModel> WarehouseList
{
get { return _WarehouseList ?? new List<InternalLookupModel>(); }
set { this._WarehouseList = value; }
}
private List<InternalLookupModel> _ScacCodeList;
public List<InternalLookupModel> ScacCodeList
{
get { return _ScacCodeList ?? new List<InternalLookupModel>(); }
set { this._ScacCodeList = value; }
}
private List<InternalLookupModel> _PackagingFormList;
public List<InternalLookupModel> PackagingFormList
{
get { return _PackagingFormList ?? new List<InternalLookupModel>(); }
set { this._PackagingFormList = value; }
}
private List<InternalLookupModel> _PackagingMaterialList;
public List<InternalLookupModel> PackagingMaterialList
{
get { return _PackagingMaterialList ?? new List<InternalLookupModel>(); }
set { this._PackagingMaterialList = value; }
}
private List<InternalLookupModel> _TransPayMethodList;
public List<InternalLookupModel> TransPayMethodList
{
get { return _TransPayMethodList ?? new List<InternalLookupModel>(); }
set { this._TransPayMethodList = value; }
}
private List<InternalLookupModel> _TransTypeList;
public List<InternalLookupModel> TransTypeList
{
get { return _TransTypeList ?? new List<InternalLookupModel>(); }
set { this._TransTypeList = value; }
}
private List<InternalLookupModel> _ServiceLevelCodeList;
public List<InternalLookupModel> ServiceLevelCodeList
{
get { return _ServiceLevelCodeList ?? new List<InternalLookupModel>(); }
set { this._ServiceLevelCodeList = value; }
}
private List<InternalLookupModel> _UnitOfMeasureList;
public List<InternalLookupModel> UnitOfMeasureList
{
get { return _UnitOfMeasureList ?? new List<InternalLookupModel>(); }
set { this._UnitOfMeasureList = value; }
}
}
請包括'ShipmentViewModel'的源代碼。我不是這方面的專家,但我懷疑你用於列表的類型可能與將對象反序列化爲你的類型的任何東西不兼容。 –
@Brian Ball:我已經按照要求在shipmentviewmodel源文件中添加了 – Adam
您應該*在* .Result'上遇到死鎖。當問題發生時檢查線程視圖。如果是這種情況,請隨意查找與「C#ASP.Net等待異步死鎖」等搜索相對應的副本。 –