简体   繁体   中英

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. No error.

I have about 80 other methods in this project with no problems, but this one will simply not convert. I could change Serializer to NewtonSoft but this is nearing the end of this project. I've never seen this issue before.

My objectX is as below, is there something I've missed?

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

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}]

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?

I believe you are not telling WCF how to return the data via the attributes on the contract. you don't tell us what the WCF Contract looks like.

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). 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). 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.

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. 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. But the straightforward 'cast' from the db value of 01/01/01 00:00 to datetime would not work.

More confusing is that it returned in tests, in XML, with no issues.

Thanks to @Mitesh for prompting me to set up a simple test that led to this 'discovery'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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