WCF Interview questions--Part 3


WCF Interview Questions on Contracts - Part 3

What is the best way to serialize Polymorphic Types in WCF?

The best way to serialize Polymorphic Types in WCF is to use KnownType attribute on the parent type as shown in the example below. CorporateCustomer and PremiumCustomer classes inherit from Customer class, and hence we can associate CorporateCustomer and PremiumCustomer types as known types in 3 different ways depending on the project requirement.

1. Associate known types to the base types themselves.

2. Associate known types to particular operations.

3. Associate known types to the service contract as a whole.

Example 1, we are associating known types, CorporateCustomer and PremiumCustomer to the base type, Customer.

In Example 2, we are associating known type, CorporateCustomer on SaveCorporateCustomer(Customer customer) and GetCorporateCustomer(int CustomerId) operations using ServiceKnownType attribute.

Example 3, we are associating known types, CorporateCustomer and PremiumCustomer to the service contract ICustomerService as a whole.

It is also possible to specify known types in a configuration file rather than in code. Example 4 shows how to specify known types in configuration.file.

No comments:

Post a Comment