简体   繁体   English

WCF服务无法返回对象列表的JSON

[英]WCF Service cannot return JSON of List of objects

I have an object defined as below. 我有一个定义如下的对象。 From my WCF Service I can return a List with no problems, but when I try to return as Json, it returns zero bytes. 从我的WCF服务中,我可以毫无问题地返回一个List,但是当我尝试以Json身份返回时,它返回的字节数为零。 No error. 没错

I have about 80 other methods in this project with no problems, but this one will simply not convert. 在这个项目中,我还有大约80种其他方法都没有问题,但是这种方法不会转换。 I could change Serializer to NewtonSoft but this is nearing the end of this project. 我可以将Serializer更改为NewtonSoft,但这已接近该项目的结尾。 I've never seen this issue before. 我从来没有见过这个问题。

My objectX is as below, is there something I've missed? 我的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; }
  }

Here is the contract: 这是合同:

    [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);

I think there is nothing wrong with Serializer, I have Web API project and I have used your object here is my method in controller 我认为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; }
    }

I am getting below result. 我得到的结果不足。

[{"CompanyName":"c1","Username":null,"BranchName":"b1","Amount":null,"Id":0,"ClientId":0,"UserId":0,"TypeId":0,"Credits":0,"InvoiceDate":"0001-01-01T00:00:00","Notes":null,"LinkedId":null,"Status":null,"DateProcessed":null,"ProcessedBy":null,"BranchId":0,"CreatedOn":null,"CreatedBy":null,"ModifiedOn":null,"ModifiedBy":null},{"CompanyName":"c2","Username":null,"BranchName":"b2","Amount":null,"Id":0,"ClientId":0,"UserId":0,"TypeId":0,"Credits":0,"InvoiceDate":"0001-01-01T00:00:00","Notes":null,"LinkedId":null,"Status":null,"DateProcessed":null,"ProcessedBy":null,"BranchId":0,"CreatedOn":null,"CreatedBy":null,"ModifiedOn":null,"ModifiedBy":null}] [{ “公司名称”: “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“:空}]

Please check your configuration if it has some restrictions. 如果有一些限制,请检查您的配置。

Hope this helps. 希望这可以帮助。

take a look here: 在这里看看:

How do I return clean JSON from a WCF Service? 如何从WCF服务返回干净的JSON?

I believe you are not telling WCF how to return the data via the attributes on the contract. 我相信您没有在告诉WCF如何通过合同中的属性返回数据。 you don't tell us what the WCF Contract looks like. 您不会告诉我们WCF合同的样子。

I have found out what was causing the issue, though it does not make sense to me. 我发现了造成此问题的原因,尽管对我而言这没有意义。 The data that populates the object comes from an azure db (sql server). 填充对象的数据来自azure db(sql服务器)。 Within a proc there, the InvoiceDate was set to the min date in error (it was incorrectly being saved from a null value and cast to 01/01/1001 00:00). 在该过程中,InvoiceDate设置为错误的最小日期(错误地将其从空值保存并转换为01/01/1001 00:00)。 Albeit still a true datetime, and it being stored in the object in memory as a true datetime, only when it was sent to the db as real date, was I able to return the object in JSON. 尽管仍然是真实的日期时间,并且将其作为真实的日期时间存储在内存中的对象中,但是只有当它作为真实日期发送到数据库时,我才能够以JSON返回该对象。

I see not reason why this should be the case. 我认为没有理由这样做。 The data is returned from the proc as 01/01/01 00:00, a true datetime value, hence I would expect c# to treat it as such. 数据从proc返回为01/01/01 00:00,这是一个真实的datetime值,因此我希望c#如此对待它。 Even when doing a test check to see if the date returned from the proc was <= DateTime.Min (c#), and setting it to DateTime.Min (again) in code, it would also work. 即使在进行测试检查以查看proc返回的日期是否为<= DateTime.Min(c#),并将其设置为代码中的DateTime.Min(再次)时,它也可以正常工作。 But the straightforward 'cast' from the db value of 01/01/01 00:00 to datetime would not work. 但是,从db值01/01/01 00:00到datetime的直接“ cast”将不起作用。

More confusing is that it returned in tests, in XML, with no issues. 更令人困惑的是,它以XML格式返回到测试中,没有任何问题。

Thanks to @Mitesh for prompting me to set up a simple test that led to this 'discovery'. 感谢@Mitesh提示我设置一个导致此“发现”的简单测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM