Data Contract Hierarchy in WCF

No.of Views994
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : WCF , Miscellaneous
In this article, you will learn about Data Contract Hierarchy in WCF
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will explain

1. Data Contract hierarchy
2. How base class and sub class are getting exposed in WCF?
3. What is Known Type attribute
4. Mixing of sterilization and DataContract

Data Contract Hierarchy

Image Loading

1. If any class in hierarchy is not attributed as [DataContract] or [serilizable] then InvalidDataContractException will occur at service run time.
2. WCF allows mixing [DataContract] and [Serilizable] attribute. But [Serilizable] should be at the root of the DataContract hierarchy.

Examples:

Image Loading

Mixing of DataContract and Serilizable

Image Loading

One problem in WCF

Follow the below code,
1. There are two classes in hierarchy
2. Base class is referring instance of derive class. This is perfectly valid.

Image Loading

 

Image Loading

Let us try to refer derive child class in base class,

Contract and classes

Image Loading

Error Service implantation

Image Loading

 

Image Loading

How to solve this problem?

KnownTypeAttribute

1. The solution is to explicitly tell WCF about the sub class to the base class.
2. This is done using KnownType Attribute class.

This class is defined as below in System.Runtime.Serilization namespace.

Image Loading

3. The KnownTypeAttribute allows designating the sub class.

Example

Image Loading

4. On the host side the KnownTypeAttribute affects all contracts and operations using the base class across all services and endpoints. This allows accepting subclass instead of base class.
5. WCF includes sub class in metadata such that client can also pass the sub class instead of base class.

ServiceKnownTypeAttribute

Image Loading

ServiceKnownTypeAttribute class

Image Loading

 

Image Loading

 

Image Loading

 

ServiceKnownType could be applied on a particular service operation or to entire service contract.That's all enjoy.thank you for reading.

Conclusion

The Data Contract hierarchy is very important, when you are work with WCF.i hope this article help to much more about it.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top