Linq To SQL, ASMX service using JSON and wrong date returned
I have been working on a site (manage reminders) and faced a crazy situation of date time shifted while trying to consume an ASMX web service using JSON.
The flow is like this:
- From browser i am calling API to insert reminder to DB , one of the properties is due date as date time. Data inserted correctly and i see that the date time is in UTC – for example customer wanted due date at 1/1/2000 10:10:00 AM (he is in GMT+2) and data saved is in 8:10:00 AM (move to UTC) – That is ok
- On another page i am calling a API to get the list of reminders, when i look at the grid that i have rendered on client side i see that there is a 2 hours difference (i am calling my own server and more over the response should have been with UTC).
i started analyzing the issue
And finally came to the root cause:
- When LINQ makes a query to the DB the date time columns are assumed to be DateTimeKind.Unspecified (this is mentioned here under System.DateTime section)
- The response from the ASMX is still being shifted to UTC
I truly don't understand this behavior, Do you!?
As a workaround, i projected a new DateTime and initialized it with the ticks of the original and UTC kind), something like this “DueDate = new DateTime(reminder.DueDate.Ticks, DateTimeKind.Utc)”
Not that it matters but my apps is fully client centric :)