CSLALight 3.8.2 fails when serializing/deserializing Business Objects with DateTimeOffset fields

CSLALight 3.8.2 fails when serializing/deserializing Business Objects with DateTimeOffset fields

Old forum URL: forums.lhotka.net/forums/t/8525.aspx

SWITCH posted on Saturday, February 13, 2010

Hi there,

I wasn't  able to log this to the Issue Tracker because of an error it reported when clicking the 'add new issue' link so I'll post it here for now...

It appears there is problem using CSLA Light 3.8.2 when the framework tries to serialize a Business Object that contains a property of type DateTimeOffset. The problem is in the Utilities class (\cslalightcs\Csla\Utilities.cs around line 320) in the XmlSerialize and XmlDeserialize methods. These methods do not pass a list of known types that include the DateTimeOffset value, so the DataContractSerializer does not know how to serialize/deserialize the Business Object graph when these methods are called. The following error is raised when a Business Object that has a DateTimeOffset Managed Property is saved:

Type 'System.Runtime.Serialization.DateTimeOffsetAdapter' with data contract name 'DateTimeOffset:http://schemas.datacontract.org/2004/07/System' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I was able to correct the problem by passing a list of known types to the DataContractSerializer constructor that included the DateTimeOffset type as follows:

internal static byte[] XmlSerialize(object graph)
  using (var buffer = new MemoryStream())
    XmlWriter writer = Csla.Serialization.Mobile.MobileFormatter.GetXmlWriter(buffer);
    DataContractSerializer dcs = new DataContractSerializer(graph.GetType(), new Type[] { typeof(DateTimeOffset)});
    dcs.WriteObject(writer, graph);
    return buffer.ToArray();

internal static T XmlDeserialize<T>(byte[] data)
  using (var buffer = new MemoryStream(data))
    XmlReader reader = Csla.Serialization.Mobile.MobileFormatter.GetXmlReader(buffer);
    DataContractSerializer dcs = new DataContractSerializer(typeof(T), new Type[] { typeof(DateTimeOffset)});
    return (T)dcs.ReadObject(reader);

This appeared to be similar to another known bug (http://www.lhotka.net/cslabugs/edit_bug.aspx?id=392) so sorry if I reported it again.

Can anyone see any problems with this workaround or can confirm if this is a bug?

Thanks, Richard


RockfordLhotka replied on Monday, February 15, 2010

That is the bug for this issue, yes.

I haven't looked into your solution in any depth - apparently Joe thought it was a harder fix, though I don't know why.

SWITCH replied on Monday, February 15, 2010

So far, the fix is working for me but it's quite possible there is more to the issue. Hope the extra info helps.

Thanks everyone!

Copyright (c) Marimer LLC