Eric Wu
Eric Wu

Reputation: 917

DateTime being returned incorrectly from WCF; still shown correctly in Front-end

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 DateTimes 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")

Correct database value

The stored procedure returns it correctly; WCF does not. For some odd reason, the value returned is an hour shifted back: Current WCF Server returning wrong value

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.

"Correct" value displayed

None of the projects, WCF or Front end have an .AddHour in the code. Checked a few times.

A few remarks:

  1. Both WCF and Front-end are currently running in different Windows Server 2016 physical machines.
  2. Both IIS have a Portuguese-BR globalization setting.
  3. Stored procedures are executed in the WCF layer, and fully returned as DataTables. Its data does not face any kind of conversion, parsing, ToStringing whatsoever in WCF. Front-end does that, but the values are wrong before such treatments are in effect.
  4. Not sure if related: For the first time in thirty five years, we're going to face a summer without our usual Daylight Saving Time Schedule (which usually runs from november to mid-february).
  5. This behavior has been spotted in several, different methods (and procedures).
  6. The code to display the DateTime is Convert.ToDateTime(dataRow[0]["DataAgendamentoFinal"]).ToString("mm") in the image above.
  7. This bug/not-bug scenario works now, but new WCF servers do not have this setting (whichever it is) enabled, so it returns DateTime (actually) correctly, only to have an hour added to the result in front-end.
  8. Database, WCF and Front-end servers are running in the same timezone (GMT-3 Brasilia) (Thanks, @Sooryan). This timezone does not consider DST.

Any pointers would be greatly appreciated.

Upvotes: 0

Views: 447

Answers (1)

Abraham Qian
Abraham Qian

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

Related Questions