datetime对象的处理WCF反序列化 [英] Handling WCF Deserialization of DateTime objects

查看:296
本文介绍了datetime对象的处理WCF反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经有了一个调度运行的应用程序调用WCF服务,以便在夜间运行作业。大量的这些包括对当前业务的最新信息。出于商业考虑,调度服务器设置为GMT,但我们的服务是在设定为纽约时间服务器上运行。



这提出了一个问题;日期被传递给我们的明确区信息.NET服务。因此,当服务讲述与一日期运行应用程序2008-11-03零时○○分00秒+ 0:00时,该服务解释,作为2008-11-02十九时00分○○秒-5:00 和事与错误的日期运行。



调度行为是第三方和硬编码,因此我们不能告诉调度省略时区偏移量。我们不希望的日期始终转换为格林尼治标准​​时间,因为有一个真实的可能性,我们的亚洲办事处将调用相同的服务,我们将回到同样的问题。



有没有一种方法来标记的DataContract,甚至在足够低的水平进行控制,以确保DateTime的实物会不明?或者是有一个日期时间的方式,以确定使用哪个原始信息来创建它,并将其转换回原始值中的后处理步骤



如果它帮助,现在我们的合同是相当简单。方法:取一个参数是从下面的类派生的类。

  [DataContract] 
公共类BaseTimeSensitiveParameters
{
[数据成员]公众的DateTime? BusinessDate;
}


解决方案

如果我理解正确的问题,你可以简单地使用 DateTime.ToUniversalTime()在服务端的后处理解决这个问题。为了您例如,这应该让你值为2008-11-03 00:00:00和KIND = DateTimeKind.Utc一个DateTime。现在,如果你需要这个相同的值,但由于本地或指定,你可以使用 DateTime.SpecifyKind(日期时间,DateTimeKind)来设置类不改变值。


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;
}

解决方案

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.

这篇关于datetime对象的处理WCF反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆