Exception when serialize class contains Dictionary and DateTimeOffset via DataContractSerializer
I try serialize via DataContractSerializer simple class by example:
public class SimpleExampleClass2
{
private Dictionary<string, string> _attributes1;
public Dictionary<string, string> Attributes1 => _attributes1;
public DateTimeOffset TestDate { get; set; }
}
Serialization code:
var test = new SimpleExampleClass2() { TestDate = DateTimeOffset.Now };
var ser = new DataContractSerializer(typeof(SimpleExampleClass2));
MemoryStream memoryStream = new MemoryStream();
ser.WriteObject(memoryStream, test);
memoryStream.Seek(0, SeekOrigin.Begin);
return File(memoryStream, "application/xml", $"test.xml");
But on line "ser.WriteObject(memoryStream, test)" I get exception:
InvalidDataContractException: Type 'System.Runtime.Serialization.DateTimeOffsetAdapter' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
Is this bug in DataContractSerializer?
@zhenlan @ViktorHofer @danmosemsft
It's not related to binary serialization with BinaryFormatter.
@zhenlan Any thoughts?
+@shmao
Issue moved to dotnet/corefx #22965 via ZenHub