Skip to content
Learn Netverks
1

SerializationException: DataContractSerializer has no knowledge of type after migrating from .NET Framework 4.7.2 to .NET 8

asked 8 hours ago by @qa-m4kbob0cdhjorfdnj22q 0 rep · 29 views

.net wcf datacontractserializer

I recently migrated a WPF application from .NET Framework 4.7.2 to .NET 8. After the migration, deserializing previously saved binary XML files (written using XmlDictionaryWriter.CreateBinaryWriter) throws a SerializationException at runtime.

    System.Runtime.Serialization.SerializationException:
    Element 'http://schemas.datacontract.org/2004/07/MyApp.DataProvider.CollectionData:Start' contains data of the 'MyApp.Managed.CollectionData:ToolPlane' data contract.

    The deserializer has no knowledge of any type that maps to this contract.
    Add the type corresponding to 'ToolPlane' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer. 

The serializer is configured with a list of known types and a custom ISerializationSurrogateProvider.

var surrogate = new MyAppSurrogate();
var knownTypes = new List<Type>
{
    typeof(ToolPlane),
};

var settings = new DataContractSerializerSettings
{
    KnownTypes = knownTypes,
    MaxItemsInObjectGraph = int.MaxValue,
    PreserveObjectReferences = true
};

serializer = new DataContractSerializer(typeof(T), settings);
serializer.SetSerializationSurrogateProvider(surrogate);

What I've tried

• ToolPlane is listed in the KnownTypes collection passed to DataContractSerializerSettings.

• The [DataContract] and [DataMember] attributes are present on ProbeConfiguration.

• Compared the xml of the start tag in both the old and new serialized file and it is same.

Has anyone encountered DataContractSerializer failing to resolve a known type by its data contract namespace after migrating from .NET Framework 4.7.2 to .NET 8, even though the type is explicitly registered in KnownTypes, the [DataContract]/[DataMember] attributes are in place, and the XML namespace in the legacy binary file is confirmed to be identical to what the migrated build produces? If so, what is the correct approach to make DataContractSerializer successfully deserialize legacy binary XML files in .NET 8 under these conditions?

Comments on this question (0)

Use comments to ask for clarification — answers go in the answer box below.

Log in to comment on this question.

0 answers

Your answer