Skip to content

Binary DataContractSerializer serialization is broken in .NET 7 for floating point data types #78913

Description

@IvanMaskaliou

Description

When migrating to .NET 7 I have error when de-serializing object which was serialized using DataContractSerializer (using BinaryWriter).

Reproduction Steps

Just run the following code:

usingSystem.Runtime.Serialization;usingSystem.Xml;namespaceConsoleApp2{[DataContract]publicclassTestObject{[DataMember]publicdoubleMinDepth{get;set;}}internalclassProgram{staticvoidMain(string[]args){//createvarinstance=newTestObject{MinDepth=100.8};//serializevarserializer=newDataContractSerializer(typeof(TestObject));varstream=newMemoryStream();varwriter=XmlDictionaryWriter.CreateBinaryWriter(stream);serializer.WriteObject(writer,instance);writer.Flush();//de-serializestream.Position=0;varreader=XmlDictionaryReader.CreateBinaryReader(stream,XmlDictionaryReaderQuotas.Max);varinstance2=serializer.ReadObject(reader)asTestObject;}}}

Expected behavior

instance2 is an instance of TestObject with MinDepth property value 100.8

Actual behavior

Excpetion:
System.Runtime.Serialization.SerializationException: 'There was an error deserializing the object of type ConsoleApp2.TestObject. The input source is not correctly formatted.'

Regression?

Worked correct in .NET 6 and earlier.

Known Workarounds

No response

Configuration

.NET 7 Windows 11 x64

Other information

This code works fine for .NET 6 and earlier. In .NET 7, System.Runtime.Serialization.SerializationException exception is raised for property of double or float data type (works fine for decimal). Also works correct when property contain integer value (for example 100, value 100.8 raises exception). Does not depend on regional settings.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions