I tried out few combinations to expolore , how DataMember are exposed to client .
First is I used base class with Serilizable attribute. And I implented service as below
[Serializable]
public class BaseData
{
[DataMember]
public int ID { get; set; }
}
[DataContract]
public class SubData:BaseData
{
[DataMember]
public int ID { get; set; }
}
}
Surprisngly at the client side , I am getting two ID , ID1 ( I know , why I am getting two ID because , I am not overriding the ID field). But I am getting ID property exposed for the subclass .
Service1Client proxy = new Service1Client();
SubData obj = new SubData();
obj.ID = 9;
obj.ID1 = 10;
Console.WriteLine(obj.ID);
Console.WriteLine(obj.ID1);
Console.ReadKey(true);
Second , when I changed the class like below
[Serializable]
public class BaseData
{
[DataMember]
public virtual int ID { get; set; }
}
[DataContract]
public class SubData:BaseData
{
[DataMember]
public override int ID { get; set; }
}
I am not getting ID for either of the class. where as I am getting Idk_backingField. I am not sure whether, it is substitution of ID property ?
Service1Client proxy = new Service1Client();
SubData obj = new SubData();
BaseData baseobj = new BaseData();
baseobj.IDk__BackingField = 10;
obj.IDk__BackingField = 10;
Console.WriteLine(obj.IDk__BackingField);
Console.WriteLine(baseobj.IDk__BackingField);
Console.ReadKey(true);
3rd I tried
I changed classes as
[DataContract]
public class BaseData
{
[DataMember]
public virtual int ID { get; set; }
}
[DataContract]
public class SubData:BaseData
{
[DataMember]
public override int ID { get; set; }
}
and at the client side , I am getting
ID property exposed for both of the classes . see the client code below.
Service1Client proxy = new Service1Client();
SubData obj = new SubData();
BaseData baseobj = new BaseData();
obj.ID = 9;
baseobj.ID = 10;
Console.WriteLine(obj.ID);
Console.WriteLine(baseobj.ID );
Console.ReadKey(true);
So Id for both of the classes are exposed.
Now coming to usual class definition
[DataContract(Namespace="http://localhost/KnownType")]
[KnownType(typeof(SubData))]
public class BaseData
{
[DataMember]
public virtual int ID { get; set; }
}
[DataContract(Namespace = "http://localhost/KnownType")]
public class SubData:BaseData
{
[DataMember]
public override int ID { get; set; }
}
At client side both Id is exposed .
Service1Client proxy = new Service1Client();
SubData obj = new SubData();
BaseData baseobj = new BaseData();
obj.ID = 9;
baseobj.ID = 10;
Console.WriteLine(obj.ID);
Console.WriteLine(baseobj.ID );
Console.ReadKey(true);
Its really funny right ? How WCF is managing it . any way , we know after reading this blog the best
way to go with DataContract Hierarchy