0

Im trying to send DateTime object from a WCF service which returns JSON objects. All dates works fine except 0001-01-01. The service fails to serialize this, why is that so?

These work fine:

return DateTime.now;
return new DateTime(1,1,2);

but these do not work:

return new DateTime();
return new DateTime(0);
return new DateTime(1,1,1);

My interface

    [OperationContract]
    [WebInvoke(Method = "GET",
       ResponseFormat = WebMessageFormat.Json,
       UriTemplate = "/GetTest")]
    DateTime GetTest();

Nothing crashes, except that no data is returned from the service. Why is that so?

Zeezer
  • 1,503
  • 2
  • 18
  • 33

2 Answers2

0

I guess that is because of the following: DateTime's representation in milliseconds? You cannot set datetime to such a value.

Community
  • 1
  • 1
  • It works fine to create a new DateTime object like this in C#: new DateTime(0). Can you specify your answer. – Zeezer Nov 12 '13 at 09:52
  • The construction you mentioned calls `DateTime.MinValue` (http://msdn.microsoft.com/en-us/library/system.datetime.minvalue(v=vs.110).aspx) –  Nov 12 '13 at 09:57
0

WCF and JSON have troubles with small dates. (everything before 1/1/1000) can match this problem.

Also, the default datetime date would be 1/1/1. This value is represented by NULL in WCF and JSON and NULL handling will result in No Result at all, this is the part where your code goes wrong i think...

When entering small dates i propose you try the following line of code:

var date = new DateTime(1,1,1,0,0,0,DateTimeKind.Utc);

(resource http://www.west-wind.com/weblog/posts/2008/Jun/23/Crashing-WCF-35-JSON-Services-with-DateTimeMinValue )

Schuere
  • 1,579
  • 19
  • 33
  • It does not return NULL. First i had a list with multiple values including DateTime objects and when 0001-01-01 occured, no results at all were returned. And when using the debugger, nothing crashes. – Zeezer Nov 12 '13 at 10:03
  • 2
    So you cant use new DateTime() with WCF and JSON? Seams like a serious problem – Zeezer Nov 12 '13 at 10:10
  • Edited the post, i meant no result is given throught the WCF. You don't see the NULL, but it is processed as such. This is the reason why you won't see any results after that. Also no errors will be thrown at you. – Schuere Nov 12 '13 at 10:10
  • 1
    Can you edit in some information as to why this problem arises for the WCF/JSON ignorant few (like me!). I have curiosity and it g-naws at me. – Gusdor Nov 12 '13 at 10:57
  • I find the resource post more helpful then my thoughts of this :s – Schuere Nov 12 '13 at 11:42