WCF 日期时间偏移兼容性
我有一个 .NET WCF 服务,其中包含一些采用 DateTimeOffset 的操作合约。这样做的目的是避免与 DST 和时区产生任何混淆。
然而,我怀疑使用 DateTimeOffset 毕竟是一个好主意,因为它相当不标准,并且会给任何试图连接 Java 应用程序或绑定到旧 .NET 版本的 .NET 应用程序的人带来麻烦。 。
另一种方法是期望使用 UTC 日期时间,但这会带来有人忘记使用 UTC 时间并使用本地时间调用服务的风险。 我还可以期望本地时间 DateTime,因为客户端将始终位于同一时区,但这在 DST 更改方面留下了一些微妙但经典的模糊性。
是否有人对服务接口中的 DateTimeOffset 有令人头疼的故事,或者毕竟使用起来相对没有问题?
I have a .NET WCF service with a few operation contracts that takes a DateTimeOffset. The idea is to avoid any confusion with DST and time zones.
However I am in doubt that using DateTimeOffset is a good idea after all, since it is fairly non-standard and would cause headaches for anyone trying to connect with, say, a Java application or a .NET application bound to an older .NET version.
An alternative is to expect a UTC DateTime, but this introduces the risk that someone will forget to use UTC time and call the service with a local time.
I could also expect a local time DateTime, since clients will always be in the same timezone, but this leaves some subtle, but classic, ambiguity around DST changes.
Does anyone have headache stories with DateTimeOffset in a service interface or is it relatively unproblematic to use after all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我目前正在将一些基础设施更改为 WCF,并偶然发现了这个悬而未决的问题,并决定尝试一下。 :)
WCF 序列化
DateTime
和DateTimeOffset
的方式似乎有点奇怪。如以下示例所示,在与其他平台一起使用时,使用DateTime
看起来是更好的选择:请求的响应 XML 为:
I'm in the GMT+01.00 timezone, so the value might be right 。为什么会这样呢?那么,WSDL 定义
Container
如下:而
DateTimeOffset
- 在 WSDL 中 - 定义为:所以基本上,
DateTime
被序列化为标准 < code>xs:dateTime (它确实具有正确的时区组件)和DateTimeOffset
被序列化为非标准复杂类型,调用者必须正确理解和处理该类型。FWIW;由于我发现了这一点,我可能会使用
DateTime
作为 WCF 接口,除非我实际上需要处理不同的时区偏移。目前,我认为支持使用复杂类型的唯一理由(因为 xs:dateTime 应该能够包含它所包含的所有信息!)是如果 xs:dateTime code> 用于序列化
DateTime
和DateTimeOffset
,WCF 客户端不知道要使用哪种类型。I'm currently changing some of our infrastructure to WCF and stumbled upon this unanswered question and decided to try it. :)
The way that WCF serializes
DateTime
andDateTimeOffset
seems to be a bit weird. As the following sample shows, usingDateTime
looks like the better option when working with other platforms:The response XML of the request is:
I'm in the GMT+01.00 timezone, so the values seem about right. Why is it this way? Well, the WSDL defines
Container
like this:And
DateTimeOffset
- in WSDL - is defined as:So basically,
DateTime
is serialized as a standardxs:dateTime
(which does have the correct timezone component) andDateTimeOffset
is serialized into a non-standard complex type, which the caller would have to understand and handle correctly.FWIW; Since I found this out, I will probably use
DateTime
for the WCF interface unless I actually need to take care of different timezone offsets.Currently, the only justification I could see in favour of using the complex type (since
xs:dateTime
should be able to contain all information that it does!) is that ifxs:dateTime
had been used to serializeDateTime
andDateTimeOffset
, a WCF client would have no idea which type to use.恕我直言,在 WCF 服务中使用 DateTime 时最令人头痛的是 WCF 目前不支持 xs:Date - 请参阅 此相关问题以及链接的 Connect 建议。
DateTimeOffset 对解决这个问题没有帮助。
IMHO the biggest headache in using DateTime with a WCF Service is that WCF doesn't currently support xs:Date - see this related question and the linked Connect suggestions.
DateTimeOffset doesn't help with this problem.