WCF DateTimeOffset兼容性

Hol*_*roe 6 .net wcf web-services datetimeoffset

我有一个.NET WCF服务,其中包含一些采用DateTimeOffset的操作契约.这个想法是为了避免与夏令时和时区混淆.

但是我怀疑使用DateTimeOffset毕竟是一个好主意,因为它是相当非标准的,并且会对尝试连接的任何人造成麻烦,例如,Java应用程序或绑定到较旧.NET版本的.NET应用程序.

另一种方法是期望UTC DateTime,但这会带来某人忘记使用UTC时间并以本地时间调用服务的风险.我还可以期待当地时间DateTime,因为客户端将始终处于相同的时区,但这会在DST更改中留下一些微妙但经典的模糊性.

是否有人在服务界面中使用DateTimeOffset头痛故事,或者毕竟使用起来相对没有问题?

han*_*ngy 9

我目前正在将一些基础设施改为WCF,偶然发现了这个未解决的问题并决定尝试一下.:)

该WCF序列化的方式DateTime,并DateTimeOffset似乎有点不可思议.如下面的示例所示,在使用DateTime其他平台时,使用看起来更好的选项:

using System;
using System.Runtime.Serialization;
using System.ServiceModel;

[ServiceContract]
public class DateTimeOffsetService
{
    [OperationContract]
    public Container DoWork()
    {
        return new Container
        {
            NowDateTime = DateTime.Now,
            UtcNowDateTime = DateTime.UtcNow,
            NowDateTimeOffset = DateTimeOffset.Now,
            UtcNowDateTimeOffset = DateTimeOffset.UtcNow
        };
    }
}

[DataContract]
public class Container
{
    [DataMember]
    public DateTime NowDateTime { get; set; }

    [DataMember]
    public DateTime UtcNowDateTime { get; set; }

    [DataMember]
    public DateTimeOffset NowDateTimeOffset { get; set; }

    [DataMember]
    public DateTimeOffset UtcNowDateTimeOffset { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

请求的响应XML是:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <DoWorkResponse xmlns="http://tempuri.org/">
      <DoWorkResult xmlns:a="http://schemas.datacontract.org/2004/07/RD.MES.WcfService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:NowDateTime>2012-03-23T15:59:47.8328698+01:00</a:NowDateTime>
        <a:NowDateTimeOffset xmlns:b="http://schemas.datacontract.org/2004/07/System">
          <b:DateTime>2012-03-23T14:59:47.8328698Z</b:DateTime>
          <b:OffsetMinutes>60</b:OffsetMinutes>
        </a:NowDateTimeOffset>
        <a:UtcNowDateTime>2012-03-23T14:59:47.8328698Z</a:UtcNowDateTime>
        <a:UtcNowDateTimeOffset xmlns:b="http://schemas.datacontract.org/2004/07/System">
          <b:DateTime>2012-03-23T14:59:47.8328698Z</b:DateTime>
          <b:OffsetMinutes>0</b:OffsetMinutes>
        </a:UtcNowDateTimeOffset>
      </DoWorkResult>
    </DoWorkResponse>
  </s:Body>
</s:Envelope>
Run Code Online (Sandbox Code Playgroud)

我在GMT + 01.00时区,所以价值似乎是正确的.为什么会这样?好吧,WSDL定义Container如下:

<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/WcfService">
    <xs:import schemaLocation="http://localhost:3608/DateTimeOffsetService.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/System"/>
    <xs:complexType name="Container">
        <xs:sequence>
            <xs:element minOccurs="0" name="NowDateTime" type="xs:dateTime"/>
            <xs:element minOccurs="0" name="NowDateTimeOffset" type="q1:DateTimeOffset"/>
            <xs:element minOccurs="0" name="UtcNowDateTime" type="xs:dateTime"/>
            <xs:element minOccurs="0" name="UtcNowDateTimeOffset" type="q2:DateTimeOffset"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="Container" nillable="true" type="tns:Container"/>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

并且DateTimeOffset- 在WSDL中 - 定义为:

<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/System">
    <xs:import schemaLocation="http://localhost:3608/DateTimeOffsetService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
    <xs:complexType name="DateTimeOffset">
        <xs:annotation>
            <xs:appinfo>
                <IsValueType>true</IsValueType>
            </xs:appinfo>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="DateTime" type="xs:dateTime"/>
            <xs:element name="OffsetMinutes" type="xs:short"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="DateTimeOffset" nillable="true" type="tns:DateTimeOffset"/>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

所以基本上,DateTime序列化为标准xs:dateTime(它具有正确的时区组件)并被DateTimeOffset序列化为非标准复杂类型,调用者必须正确理解和处理.

FWIW; 因为我发现了这个,我可能会DateTime用于WCF接口,除非我真的需要处理不同的时区偏移.

目前,唯一的理由我可以看到赞成使用复杂类型的(因为xs:dateTime应该能够包含它的所有信息!)是,如果xs:dateTime已经使用序列化DateTimeDateTimeOffset,WCF客户端将不知道要使用哪种类型.


Joe*_*Joe 1

恕我直言,在 WCF 服务中使用 DateTime 时最令人头痛的是 WCF 目前不支持 xs:Date - 请参阅此相关问题和链接的连接建议。

DateTimeOffset 对解决这个问题没有帮助。