Search code examples
.netwcfdatacontract

KnownType Serialization Issue


Having read the documentation and many many articles I believe the following should work but it doesn't.

This is how my datacontracts are structured.

[DataContract]
[KnownType(typeof(Friend))]
public class Person
{
   private string name;

   [DataMember]
   public string Name { get { return name; } set { name = value; }}

   private Place location;

   [DataMember]
   public Place Location { get { return location; } set { location = value; }}
}

[DataContract]
public class Friend : Person
{
   private int mobile;

   [DataMember]
   public int Mobile { get { return mobile; } set { mobile = value; }}
}

[DataContract]
[KnownType(typeof(City))]
public class Place
{
   private int altitude;

   [DataMember]
   public int Altitude { get { return altitude; } set { altitude = value; }}
}

[DataContract]
public class City : Place
{
   private int zipCode;

   [DataMember]
   public int ZipCode { get { return zipCode; } set { zipCode = value; }}
}

The client sends the following example object:

Person tom = new Friend();
tom.Name = "Tom";

Place office = new City();
office.Altitude = 500;
office.ZipCode = 900500;

tom.Location = office;

The issue is for some reason none of the Place values are serialized.

What mistake am I making?

Thanks.


Solution

  • After much frustration turns out my datacontract design was flawed :(