[英]WCF Service cannot return JSON of List of objects
我有一個定義如下的對象。 從我的WCF服務中,我可以毫無問題地返回一個List,但是當我嘗試以Json身份返回時,它返回的字節數為零。 沒錯
在這個項目中,我還有大約80種其他方法都沒有問題,但是這種方法不會轉換。 我可以將Serializer更改為NewtonSoft,但這已接近該項目的結尾。 我從來沒有見過這個問題。
我的objectX如下,我錯過了什么嗎?
public class objectX
{
public string CompanyName { get; set; }
public string Username { get; set; }
public string BranchName { get; set; }
public Nullable<decimal> Amount { get; set; }
public int Id { get; set; }
public int ClientId { get; set; }
public int UserId { get; set; }
public int TypeId { get; set; }
public int Credits { get; set; }
public System.DateTime InvoiceDate { get; set; }
public string Notes { get; set; }
public Nullable<int> LinkedId { get; set; }
public string Status { get; set; }
public Nullable<System.DateTime> DateProcessed { get; set; }
public Nullable<int> ProcessedBy { get; set; }
public int BranchId { get; set; }
public Nullable<System.DateTime> CreatedOn { get; set; }
public Nullable<int> CreatedBy { get; set; }
public Nullable<System.DateTime> ModifiedOn { get; set; }
public Nullable<int> ModifiedBy { get; set; }
}
這是合同:
[OperationContract]
[WebInvoke(Method = "GET",
UriTemplate = "objectX/Read/{clientId}/{branchId}/{statuses}/{dateTime}",
ResponseFormat = WebMessageFormat.Json)]
List<objectX> objectXRead(string clientId, string branchId, string statuses, string dateTime);
我認為Serializer沒什么問題,我有Web API項目,並且在這里使用了您的對象,這是控制器中的方法
public List<objectX> GetObjectX()
{
return new List<objectX>() { new objectX() { CompanyName = "c1", BranchName = "b1" },
new objectX() { CompanyName = "c2", BranchName = "b2" }};
}
public class objectX
{
public string CompanyName { get; set; }
public string Username { get; set; }
public string BranchName { get; set; }
public Nullable<decimal> Amount { get; set; }
public int Id { get; set; }
public int ClientId { get; set; }
public int UserId { get; set; }
public int TypeId { get; set; }
public int Credits { get; set; }
public System.DateTime InvoiceDate { get; set; }
public string Notes { get; set; }
public Nullable<int> LinkedId { get; set; }
public string Status { get; set; }
public Nullable<System.DateTime> DateProcessed { get; set; }
public Nullable<int> ProcessedBy { get; set; }
public int BranchId { get; set; }
public Nullable<System.DateTime> CreatedOn { get; set; }
public Nullable<int> CreatedBy { get; set; }
public Nullable<System.DateTime> ModifiedOn { get; set; }
public Nullable<int> ModifiedBy { get; set; }
}
我得到的結果不足。
[{ “公司名稱”: “C1”, “用戶名”:NULL, “BRANCHNAME”: “B1”, “金額”:空, “ID”:0 “客戶端Id”:0, “用戶ID”:0,“TYPEID “:0,” 積分 “:0,” InvoiceDate “:” 0001-01-01T00:00:00" , “注意事項”:空, “LinkedId”:空, “狀態”:空, “DateProcessed”:空, “ProcessedBy”:NULL, “BranchId”:0 “CreatedOn”:NULL, “CreatedBy”:NULL, “ModifiedOn”:NULL, “ModifiedBy”:空},{ “公司名稱”: “C2”, “用戶名”:空, “BRANCHNAME”: “B2”, “金額”:空, “ID”:0 “客戶端Id”:0, “用戶ID”:0, “TYPEID”:0, “積分”:0, “InvoiceDate”: “0001-01-01T00:00:00”, “注意事項”:空, “LinkedId”:空, “狀態”:空, “DateProcessed”:空, “ProcessedBy”:空, “BranchId”:0,“CreatedOn “:NULL,” CreatedBy “:NULL,” ModifiedOn “:NULL,” ModifiedBy“:空}]
如果有一些限制,請檢查您的配置。
希望這可以幫助。
我發現了造成此問題的原因,盡管對我而言這沒有意義。 填充對象的數據來自azure db(sql服務器)。 在該過程中,InvoiceDate設置為錯誤的最小日期(錯誤地將其從空值保存並轉換為01/01/1001 00:00)。 盡管仍然是真實的日期時間,並且將其作為真實的日期時間存儲在內存中的對象中,但是只有當它作為真實日期發送到數據庫時,我才能夠以JSON返回該對象。
我認為沒有理由這樣做。 數據從proc返回為01/01/01 00:00,這是一個真實的datetime值,因此我希望c#如此對待它。 即使在進行測試檢查以查看proc返回的日期是否為<= DateTime.Min(c#),並將其設置為代碼中的DateTime.Min(再次)時,它也可以正常工作。 但是,從db值01/01/01 00:00到datetime的直接“ cast”將不起作用。
更令人困惑的是,它以XML格式返回到測試中,沒有任何問題。
感謝@Mitesh提示我設置一個導致此“發現”的簡單測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.