Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

c# - Handling WCF Deserialization of DateTime objects

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...