WCF Learning (v): Known types of data contracts

Source: Internet
Author: User
Tags serialization

Preparation technology:

1.c# Basic Knowledge

2. Learn about WCF Basics

In normal C # development We are allowed to replace the base class with subclasses, which is also known as the substitution principle. But we do not replace the parent class with the subclass of the data contract in WCF because there is a problem with serialization. As an example:

We have data contracts:

Class employee{...}

In the service contract:

Interface Iemployeemanager
void AddEmployee (employee employee);

Then we have a class in the proxy of the client: intern inherits from

Class intern:employee{...}

And then when the client calls:

Proxy. AddEmployee (new Intern ()) is an error. Because the Intern object is not recognized on the server side because he cannot deserialize intern into an Employee object (WCF serialization).

WCF offers us a solution by using the KnownTypeAttribute feature to identify the corresponding subclass on the base class. The KnownTypeAttribute feature can be used on struct and class. Example:

[Knowntype (typeof (Customer))]
Class employee{...}
Class intern:employee{...}

So that we can cross all the services and endpoints on all contracts and operations, allowing the service to accept subclasses. But this will encounter a problem, we can not go to specify a certain service operation, so the Knowntype flaw is too wide. WCF provides another attribute--serviceknowntype.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.