WCF Service – What is a KnownType?

By | May 31, 2012

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 {}

Now, look into following code:

  [DataContract]
  [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.

Category: Uncategorized Tags: ,

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.