KnownType Attribute in WCF: Part 6

In this article, we learn what the KnownType attribute is in WCF and how to implement it.

Before reading this article, I highly recommend reading my previous parts:
According to MSDN the KnownTypeAttribute class allows you to specify, in advance, the types that should be included for consideration during deserialization. The WCF service generally accepts and returns the base type. If you expect the service to accept and return an inherited type then we use the knowntype attribute. In other words, by default you cannot use a subclass of the data contract class instead of its base class. We need to tell explicitly to WCF about the subclass using the knowntype attribute. Let me explain with an example.
 
Create a WCF application and implement the following datacontract.
  1. using System.Runtime.Serialization;  
  2. using System.ServiceModel;  
  3.   
  4. namespace WcfDemo  
  5. {  
  6.     [ServiceContract]  
  7.     public interface IBuilder  
  8.     {  
  9.         [OperationContract]  
  10.         Builder GetBuilder();  
  11.   
  12.         [OperationContract]  
  13.         Builder GetBuilderById(Builder builder);  
  14.     }  
  15.       
  16.       
  17.     [DataContract]  
  18.     public class Builder  
  19.     {  
  20.         [DataMember]  
  21.         public int BuilderId { getset; }  
  22.   
  23.         [DataMember]  
  24.         public string BuilderName { getset; }  
  25.   
  26.         [DataMember]  
  27.         public string MobileNo { getset; }  
  28.   
  29.         public enum BuilderType { DayBasic = 1, ContractBasis = 2 }  
  30.     }  
  31.   
  32.     public class DayBasic : Builder  
  33.     {  
  34.         public int LabourCharges { getset; }  
  35.         public int WorkedDay { getset; }  
  36.         public enum LabourType { Fitter =1, Helper =2}  
  37.     }  
  38.   
  39.     public class ContractBasis : Builder  
  40.     {  
  41.         public int TotalAmount { getset; }  
  42.         public int DayComplited { getset; }  
  43.     }  
  44. }  
As I said above we can not use a subclass of the data contract class instead of its base class. To do that I will use KnownTypeAttribute.
  1. using System.Runtime.Serialization;  
  2. using System.ServiceModel;  
  3.   
  4. namespace WcfDemo  
  5. {  
  6.     [ServiceContract]  
  7.     public interface IBuilder  
  8.     {  
  9.         [OperationContract]  
  10.         Builder GetBuilder();  
  11.   
  12.         [OperationContract]  
  13.         Builder GetBuilderById(Builder builder);  
  14.     }  
  15.       
  16.     [KnownType(typeof(DayBasic))]  
  17.     [KnownType(typeof(ContractBasis))]  
  18.     [DataContract]  
  19.     public class Builder  
  20.     {  
  21.         [DataMember]  
  22.         public int BuilderId { getset; }  
  23.   
  24.         [DataMember]  
  25.         public string BuilderName { getset; }  
  26.   
  27.         [DataMember]  
  28.         public string MobileNo { getset; }  
  29.   
  30.         public enum BuilderType { DayBasic = 1, ContractBasis = 2 }  
  31.     }  
  32.   
  33.     public class DayBasic : Builder  
  34.     {  
  35.         public int LabourCharges { getset; }  
  36.         public int WorkedDay { getset; }  
  37.         public enum LabourType { Fitter =1, Helper =2}  
  38.     }  
  39.   
  40.     public class ContractBasis : Builder  
  41.     {  
  42.         public int TotalAmount { getset; }  
  43.         public int DayComplited { getset; }  
  44.     }  
  45. }  
Happy coding!