ARTICLE

Generic Data Access Layer for WCF : Part 5

Posted by Mahadesh Mahalingappa Articles | WCF with C# September 20, 2011
In this article, we will solve the runtime serialization issues we encountered in the previous article.
Reader Level:
Download Files:
 

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 functionali 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 recognise 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 recognised type and WSDL will not through a Seralisation error as it was doing earlier .


To know more about it read this http://msdn.microsoft.com/en-us/library/ms730167.aspx

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 :

    [KnownType(typeof(Article))]
    [Serializable()]
    [DataContractAttribute(IsReference = true)]
    public class TEntity : EntityObject
    { 

    }

Now when we run the Service we are able to access the data .

DalWCF.gif

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.


  

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.