Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling WCF Deserialization of DateTime objects

Tags:

c#

datetime

wcf

We've got a scheduling application running that calls a WCF service to run nightly jobs. A large number of these include information about the current business date. For business reasons the scheduling server is set to GMT, but our service is running on servers set to NY time.

This raises a problem; the dates are being passed to our .NET service with explicit timezone information. So when the service tells the application to run with a date of "2008-11-03 00:00:00 +0:00", the service interprets that as "2008-11-02 19:00:00 -5:00" and things run with the wrong date.

The scheduler behavior is third-party and hard coded, so we can't tell the scheduler to omit the timezone offset. We don't want to always convert the date to GMT because there's a real possibility that our asian offices will call the same service and we'll be back to the same problem.

Is there a way to flag the DataContract, or even control it at a low enough level to make sure the DateTime Kind will be Unspecified? Or is there a way with a DateTime to determine what the original information used to create it was and convert it back to the original value in a post-processing step?

If it helps, right now our contract is fairly simple. Methods take one parameter which is a class derived from the class below.

[DataContract]
public class BaseTimeSensitiveParameters
{
    [DataMember] public DateTime? BusinessDate;
}
like image 503
Hounshell Avatar asked Nov 03 '08 15:11

Hounshell


1 Answers

If I understand the problem correctly, you could solve this in post processing by simply using DateTime.ToUniversalTime() on the service side. For your example this should get you a DateTime with the value "2008-11-03 00:00:00" and Kind=DateTimeKind.Utc. Now if you need this same value, but as Local or Unspecified, you could use DateTime.SpecifyKind(DateTime, DateTimeKind) to set the Kind without changing the value.

like image 89
csgero Avatar answered Oct 29 '22 20:10

csgero