Generic Data Access Layer for WCF : Part 5

In this article, we will resolve the runtime serialization issues that we encountered in our previous article Generic Data Access Layer for WCF : Part 4.
WCF uses WSDL to expose its functionality does not recognize .NET types because of WSDL limitations. However, WCF has a concept called the Known Types and using this concept, we can transfer types. This is the known issue of WCF serialization.
When data arrives at a receiving endpoint, the WCF runtime attempts to deserialize the data into an instance of a common language runtime (CLR) type. The type that is instantiated for deserialization is chosen by first inspecting the incoming message to determine the data contract to which the contents of the message conform. The deserialization engine then attempts to find a CLR type that implements a data contract compatible with the message contents. The set of candidate types that the deserialization engine allows for during this process is referred to as the deserializer's set of "known types."
Through Known Types WCF tells the WSDL that this is a known type and please recognize it. It order to set this Known Type WCF offers KnownTypeAttribute.
The KnownTypeAttribute class allows you to specify, in advance, the types that should be included for consideration during deserialization.
Once we use this attribute on top of the TEntity class, we tell the WSDL that this is a recognized type and WSDL will not through a Serialisation error as it was doing earlier.
To achieve this we need to go back again to our Data Access Layer and modify the decoration on the TEntity class as shown below :
  1. [KnownType(typeof(Article))]  
  2. [Serializable()]  
  3. [DataContractAttribute(IsReference = true)]  
  4. public class TEntity : EntityObject  
  5. {   
  7. }  
Now when we run the Service we are able to access the data.
I have uploaded the latest code with this article. What we have achieved is good but it can be still better. I am working on achieving Generics at runtime. I will share once I am done. I would modify this code then.
I hope you like this article.

Similar Articles