0
votes

As per MSDN KnownTypes Specifies known types to be used by a service when serializing or deserializing. I am trying to serialize using DataContractSerializer.

[KnownType(typeOf(A))]
Class A: ISomeInterface
{

}

Class B: 
{
   public A{get;Set;}
} 

Its working fine when I am serializing but throws below exception while deserializing.

Error in line 1 position 420. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/SCS.Domain.FormsAndRecordsManagement.Contract.Base:A'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'A' 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.

1

1 Answers

0
votes

You are doing it in a wrong way. KnownType allows you to specify, in advance, the types that should be included for consideration during deserialization.

[KnownType(typeOf())] attribute is use as annotation to include the other dependency of contract with other object.

Sample, if class B has dependency to class A you should annotate class B like this way:

[DataContract]
public class A : ISomeInterface
{

}

[KnownType(typeOf(A))]
[DataContract]
public Class B: 
{
   [DataMember]
   public A { get; set;}
}