Reputation: 917
We're deploying new servers to run our WCFs, and ran into an ... unusual problem in our current, production servers .
TLDR;
Something in our WCF and Front-end servers is making them shift DateTime
s stored in databases back and forth. Please help me find out what.
We have a DateTime filled in a SQL Server 2016 table/column, like this:
(DataAgendamento stands for "Scheduling Date")
The stored procedure returns it correctly; WCF does not. For some odd reason, the value returned is an hour shifted back:
Things get weirder still: Our front-end application (running ASP.NET WebForms in .NET Framework 4.0) receives this value from the WCF and... adds an hour back. So, in the end, we have the value "correctly" displayed.
None of the projects, WCF or Front end have an .AddHour
in the code. Checked a few times.
A few remarks:
DataTable
s. Its data does not face any kind of conversion, parsing, ToString
ing whatsoever in WCF. Front-end does that, but the values are wrong before such treatments are in effect.Convert.ToDateTime(dataRow[0]["DataAgendamentoFinal"]).ToString("mm")
in the image above.DateTime
(actually) correctly, only to have an hour added to the result in front-end.Any pointers would be greatly appreciated.
Upvotes: 0
Views: 447
Reputation: 7522
When WCF is called across time zones (WCF server-side and client-side are not in the same time zone), WCF automatically converts the time fetched from the server to the local time of the client. As a distributed framework, it is sensible that WCF do this conversion. But sometimes the business logic may not need such time conversion, we can change the DateTime field to string(convert to string on the server-side), which can avoid the problem of time automatic conversion. Or use UTC time. Here are some discussions on this subject.
WCF converting time based on timezone automatically
https://social.msdn.microsoft.com/Forums/en-US/36ae825a-ffc6-4ac3-9981-c82692039d58/wcf-is-translating-my-datetime-values-across-different-time-zones?forum=wcf
https://social.msdn.microsoft.com/Forums/office/en-US/a63d0409-a8ec-4a83-8c82-f6c39356d96e/disabling-automatic-time-conversion-in-wcf?forum=wcf
Feel free to let me know if there is anything I can help with.
Upvotes: 0