What is KnownType in WCF

I am learning wcf. therefore, I come across a wcf attribute called a known type. here i got a piece of code that is not clear.

[DataContract]
   public class UserAccount {}

   [DataContract]
   public class Admin : UserAccount {}

   [DataContract]
   public class Guest : UserAccount {}

[DataContract]
  [ServiceKnownType(typeof(Admin))]
  [ServiceKnownType(typeof(Guest))]
  public class SecurityInfo
  {
          [DataMember]
          private UserAccount user;
  }

now the textbook says that the code will work fine, either we set the SecurityInfo data member for Admin, or Guest.But, if KnownTypeAttribute is not provided for the administrator and guest, the deserialization mechanism will not recognize the Admin and Guest types and will cry.

What is the relationship between SecurityInfo and the administrator and guest class? I really don't understand what a known type attribute does here. I am not familiar with the known type attribute and do not know what it does and when to use it.

, , , .... , , .

+4
2

( , , WCF).

, , WCF, . ( ), . XML. XML, , .

<data>
  <SecurityInfo>
    <user type="UserAccount">(some further XML data here)</user>
  </SecurityInfo>
</data>

, API WCF XML #, . Securityinfo UserAccount. , ?

, <user> node . , , Admin? - :

<data>
  <SecurityInfo>
    <user type="Admin">(some further XML data here)</user>
  </SecurityInfo>
</data>

[KnownType] WCF , " " Admin, UserAccount.

, Admin , public string AdminEmail { get;set; }. . WCF , Admin [KnownType].

+7

WCF , , . , ( UserAccount), , UserAccount: Admin Guest. .

0

Source: https://habr.com/ru/post/1535387/


All Articles