In Windows Communication Foundation, a KnownType is the acceptable derived class for a given Data Contract.
For the purpose of understanding in this WCF Tutorial, lets take an example. Consider the following Data Contract classes having inheritance relationship as follows:
[DataContract]
public class UserAccount {}
[DataContract]
public class Admin : UserAccount {}
[DataContract]
public class Guest : UserAccount {}
public class UserAccount {}
[DataContract]
public class Admin : UserAccount {}
[DataContract]
public class Guest : UserAccount {}
Now, look into following code:
[DataContract]
[ServiceKnownType(typeof(Admin))]
[ServiceKnownType(typeof(Guest))]
public class SecurityInfo
{
[DataMember]
private UserAccount user;
}
[ServiceKnownType(typeof(Admin))]
[ServiceKnownType(typeof(Guest))]
public class SecurityInfo
{
[DataMember]
private UserAccount user;
}
Above code will work fine, either we set SecurityInfo data member to Admin or Guest.
But if KnownTypeAttribute for Admin and Guest are not provided, deserialization engine will not recognize Admin and Guest types and will cry.
For more detailed understanding with example of all possible ways of associating Known Types in WCF, Click Here.