0

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);
6
  • Is the List<objectX> empty? Commented Jan 26, 2015 at 15:13
  • @DerekW currently it contains 7 objectX, yet nothing is returned at all, and viewing in Fiddler shows zero bytes. That is by adding a watch I can see that it is populated, but when the WCF returns the collection... well, it doesn't. Commented Jan 26, 2015 at 15:19
  • 1
    Show the WCF contract you are using.... Commented Jan 26, 2015 at 15:51
  • @Ahmedilyas, I added the OperationContract Commented Jan 26, 2015 at 16:45
  • did you try decorating the class and its properties with the correct contract attributes? i.e DataContract etc... ) Commented Jan 26, 2015 at 17:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Could be. Though to date there are a total of 432 contracts in the services I've set-up (it's a large project) and this is the first time I've came across this error. Too much wood to see the trees?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.