WCF Tutorial - Serializing and Transmitting Base Types

DZone 's Guide to

WCF Tutorial - Serializing and Transmitting Base Types

· ·
Free Resource

By default, you cannot transfer an object as its base type between a WCF server and client. The DataContractSerializer will throw an exception when it attempts to serialize the base class, but instead finds your derived class. In this tutorial, I'm going to show you how to add attributes to your base class that will allow this functionality to exist.

In my example, I'm going to create a pretty standard object hierarchy. Person <- Employee <- Manager.

public  class Person
public string Name { get; set; }

public class Employee : Person
public string EmployeeID { get; set; }

public class Manager : Employee
public List<Employee> Employees { get; set; }

As you can see, Person is the base class, Employee derives from Person, and Manager derives from Employee. In my example WCF service contract, I want to send a collection of all the people. The easiest way to do this would be to send a List of the base type, Person.

Person person = new Person();
person.Name = "Person";

Employee employee = new Employee();
employee.Name = "Employee";

Manager manager = new Manager();
manager.Name = "Manager";

List<Person> people = new List<Person>()

In order to simulate sending this collection, I'm going to directly invoke the DataContractSerializer, which is what WCF uses when serializing objects.

DataContractSerializer testSerializer = new DataContractSerializer(typeof(List<Person>));
using (MemoryStream stream = new MemoryStream())
testSerializer.WriteObject(stream, people);

If we execute this code, we'll receive the following exception:

Type 'WCFSerializationErrors.Employee' with data contract name 'Employee:http://schemas.datacontract.org/2004/07/WCFSerializationErrors' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

We got this exception because the DataContractSerializer was excepting a Person object and we gave it an Employee object. We have to tell the serializer about other known types before they can be serialized correctly. We do this through the KnownTypeAttribute. These attributes should be applied to your base class.

public class Person
public string Name { get; set; }

Now the serializer recognizes the types Employee and Manager, and will serialize them when passed as their base type. That's all we have to do. If we attempt to serialize the data again, everything will work as expected. Hopefully this tutorial shed some light on the DataContractSerializer and using this trick can help reduce the complexity and increase the cleanliness of WCF service contracts.


Published at DZone with permission of Charlie Key . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}