WCF DateTime and DateTimeOffset on the wire

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

The .NET framework 3.5 introduces the new type DateTimeOffset. This type is implemented in the mscorlib.dll and it quasi extends the DateTime type for the property Offset, which is of type short. Because the DateTime type of .NET 2.0 is not quite useful in Web Service scenarios, I tested the new one.
Here is the example:

For this example following type has been used, which encapsulate both types DatTime and DateTimeOffset:

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]

[System.ServiceModel.MessageContractAttribute(WrapperName = "GetHolidayRequest", WrapperNamespace = "http://Daenet.CalendarService/2007/08", IsWrapped = true)]

public partial class GetHolidayRequest

{

 

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://Daenet.CalendarService/2007/08", Order = 0)]

    public string Date;

 

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://Daenet.CalendarService/2007/08", Order = 1)]

    public System.DateTimeOffset DateOffset;

 

    public GetHolidayRequest()

    {

    }

 

    public GetHolidayRequest(string Date, System.DateTimeOffset DateOffset)

    {

        this.Date = Date;

        this.DateOffset = DateOffset;

    }

}

And here is the client code:

            GetHolidayRequest req = new GetHolidayRequest();

            req.Date = DateTime.Now.ToString();

            req.DateOffset = DateTime.Now;

 

            client.GetHolidays(DateTime.Now.ToString(), DateTimeOffset.Now);

 

            client.GetHolidays(XmlConvert.ToString(DateTime.Now.ToUniversalTime(), 
            XmlDateTimeSerializationMode.Local), DateTimeOffset.Now);

This code enforces WCF to sends two different messages.
Here is the result for both messages respective:

 

<MessageLogTraceRecord>

  <HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">

    <Method>POST</Method>

    <QueryString></QueryString>

    <WebHeaders>

      <VsDebuggerCausalityData>uIDPo7LpjCEdrp1Nil9FQsL8K00AAAAAJrB+6H6GakmqZaxnILapUc2qVlMrR71AmtK8q2tWSH4ACAAA</VsDebuggerCausalityData>

      <SOAPAction>"http://Daenet.CalendarService/2007/08/GetHolidays"</SOAPAction>

      <Connection>Keep-Alive</Connection>

      <Content-Length>468</Content-Length>

      <Content-Type>text/xml; charset=utf-8</Content-Type>

      <Expect>100-continue</Expect>

      <Host>dado-nb1</Host>

    </WebHeaders>

  </HttpRequest>

  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

    <s:Header>

      <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://dado-nb1/Temporary_Listen_Addresses/CalendarService</To>

      <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://Daenet.CalendarService/2007/08/GetHolidays</Action>

    </s:Header>

    <s:Body>

      <GetHolidayRequest xmlns="http://Daenet.CalendarService/2007/08">

        <Date>8/9/2007 2:57:18 PM</Date>

        <DateOffset xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema">

          <DateTime i:type="x:dateTime" xmlns="">2007-08-09T12:57:18.7612886</DateTime>

          <OffsetMinutes i:type="x:short" xmlns="">120</OffsetMinutes>

        </DateOffset>

      </GetHolidayRequest>

    </s:Body>

  </s:Envelope>

</MessageLogTraceRecord>

 

 

 

<MessageLogTraceRecord>

  <HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">

    <Method>POST</Method>

    <QueryString></QueryString>

    <WebHeaders>

      <VsDebuggerCausalityData>uIDPo7PpjCEdrp1Nil9FQsL8K00AAAAAJrB+6H6GakmqZaxnILapUc2qVlMrR71AmtK8q2tWSH4ACAAA</VsDebuggerCausalityData>

      <SOAPAction>"http://Daenet.CalendarService/2007/08/GetHolidays"</SOAPAction>

      <Content-Length>482</Content-Length>

      <Content-Type>text/xml; charset=utf-8</Content-Type>

      <Expect>100-continue</Expect>

      <Host>dado-nb1</Host>

    </WebHeaders>

  </HttpRequest>

  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

    <s:Header>

      <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://dado-nb1/Temporary_Listen_Addresses/CalendarService</To>

      <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://Daenet.CalendarService/2007/08/GetHolidays</Action>

    </s:Header>

    <s:Body>

      <GetHolidayRequest xmlns="http://Daenet.CalendarService/2007/08">

        <Date>2007-08-09T14:57:20.0766149+02:00</Date>

        <DateOffset xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema">

          <DateTime i:type="x:dateTime" xmlns="">2007-08-09T12:57:20.0766149</DateTime>

          <OffsetMinutes i:type="x:short" xmlns="">120</OffsetMinutes>

        </DateOffset>

      </GetHolidayRequest>

    </s:Body>

  </s:Envelope>

</MessageLogTraceRecord>

Conclusion:

  1. The DateTime type is serialized without of TimeZone information. Because of that in many cases this type should not be used on the wire.
  2. The DateTimeOffset type seems to be ideal for serialization purposes, because it contains the TimeZone information. However this type is not useful on clients, which do not have .NET 3.5 installed and clients build on the top of other platforms. Who knows, .NET 4.0 may have some new better type?!

Posted Aug 09 2007, 03:14 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.