how to support datetime datamember or I am on a wrong way?

First I define a datacontract:

  [DataContract]

  public class Order {

    private DateTime mTime;

    [DataMember]

    public DateTime ServerTime{

       get  { return this.mTime;}

       set  { this.mTime = value;}

    } 

  }

Then in a service I use it like this:

    public service : IService {

       public Order[ ] GetOrders(){

          Order[ ] orders = new Order[ 1];

          Order order = new Order();

          order.ServerTime = DateTime.Now;

          orders[0] = order;

          return orders;

        }

    }

Then I generate the proxy and use it in client window:

    private void OnFormLoad(object sender, EventArgs e){

        MyProxy proxy = new MyProxy();

        Order order= (proxy.GetOrders())[0];

         MessageBox.Show(order.ServerTime);

    }

Here I get an exception which says: The Added or Subtracted value results in an un-representable DateTime. But If I change the DateTime type to other types like string, this exception won't happen. Strange

 

 

 




Answer this question

how to support datetime datamember or I am on a wrong way?

  • xyzpaul

    Did you trace the value returned by the server What is inside the XML stream

    Or, try putting a breakpoint in the client and service, see what is set, and what is returned exactly. Post that information here for more information.



  • BrianM

    I hope to see the demo soon:)

  • zzy

    Im able to reproduce this, simple service contract with

    [OperationContract]
    string Test3(DateTime dt);

    Calling it with DateTime.Now yields this trace on the server (abbreviated)

    Call comes in:

    <s:Body>
    <Test3 xmlns="http://foo/bar">
    <dt>2006-01-29T18:59:43.3984368+13:00</dt>
    </Test3>
    </s:Body>

    which results in

    <Exception>
    <ExceptionType>System.ArgumentOutOfRangeException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
    <Message>
    The added or subtracted value results in an un-representable DateTime.
    Parameter name: t
    </Message>
    <StackTrace>
    at System.DateTime.op_Subtraction(DateTime d, TimeSpan t)
    at System.Xml.XmlConverter.TryParseDateTime(Byte[] chars, Int32 offset, Int32 count, DateTime&amp;amp; result)
    at System.Xml.XmlConverter.ToDateTime(Byte[] buffer, Int32 offset, Int32 count)
    at System.Xml.ValueHandle.ToDateTime()
    at System.Xml.XmlBaseReader.ReadContentAsDateTime()
    at System.Xml.XmlDictionaryReader.ReadElementContentAsDateTime()
    at System.ServiceModel.PrimitiveOperationFormatter.PartInfo.ReadValue(XmlDictionaryReader reader)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeRequest(XmlDictionaryReader reader, Object[] parameters)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage5(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage4(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage3(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage2(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage1(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.MessageRpc.Process(Boolean isOperationContextSet)
    </StackTrace>
    <ExceptionString>
    System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.
    Parameter name: t
    at System.DateTime.op_Subtraction(DateTime d, TimeSpan t)
    at System.Xml.XmlConverter.TryParseDateTime(Byte[] chars, Int32 offset, Int32 count, DateTime&amp;amp; result)
    at System.Xml.XmlConverter.ToDateTime(Byte[] buffer, Int32 offset, Int32 count)
    at System.Xml.ValueHandle.ToDateTime()
    at System.Xml.XmlBaseReader.ReadContentAsDateTime()
    at System.Xml.XmlDictionaryReader.ReadElementContentAsDateTime()
    at System.ServiceModel.PrimitiveOperationFormatter.PartInfo.ReadValue(XmlDictionaryReader reader)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeRequest(XmlDictionaryReader reader, Object[] parameters)
    at System.ServiceModel.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage5(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage4(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage3(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage2(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.DispatchRuntime.ProcessMessage1(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.MessageRpc.Process(Boolean isOperationContextSet)
    </ExceptionString>
    </Exception>

    Making the call with new DateTime( 2006, 1, 29 ) works fine, with the body looking like:

    <s:Body>
    <Test3 xmlns="http://foo/bar">
    <dt>2006-01-29T00:00:00</dt>
    </Test3>
    </s:Body>

    Any thoughts First off I would wonder why its needing to do any subtraction :)

  • esparagus

    Having a quick drill down with Reflector into System.Runtime.Serialization where TryParseDateTime lives it looks like a legit bug - this is the offending block of code:

    if (flagIndicatingPlusMinusTZOffset)
    {
    TimeSpan offset = new TimeSpan(offsetHour, offsetMinute, 0);
    if (time1 < (DateTime.MaxValue - offset))
    {
    DateTime time2 = time1.Add(offset);
    time1 = time2.ToLocalTime();
    }
    else
    {
    DateTime time3 = time1.ToLocalTime();
    time1 = time3.Add(offset);
    }
    }

    Earlier in the code, offsetHour is made negative if you are in a positive offset (e.g. the +13 becomes -13), so this causes DateTime.MaxValue + 13H, giving the exception.

    Thoughts

  • Pelucho

    i will try and post my feedback here.

  • oz123

    Thanks for bringing this to our attention. This is a known bug that has been fixed subsequently. Next CTP should have this bug fixed. Until then you can use one of the following work arounds:

    1. Consider having a private property that exposes date time as string
    2. Consider converting the dateTime to UTC
    3. Consider switching to the XmlSerializerFormat mode.

    I hope this solves your problem.

    Thanks


  • llsspatt

    well, I find an interesting thing. If I change the line

       order.ServerTime = DateTime.Now;

    To

      order.ServerTime = new DateTime(2006,1,7);

    Everything is OK.

    What's the difference



  • how to support datetime datamember or I am on a wrong way?