Should there be any problem passing this kind of a collection in WCF?
class Parent
{
[DataMember]
// some data members
[DataMember]
Child myChild;
}
class Child : Parent
{
[DataMember]
// some more data members
[DataMember]
Parent myParent;
}
Should there be any problem passing a list of Parent?
I get strange results, sometimes the channel faults, sometimes it doesn’t fault but gives me no data until I remove all the children from the list.
First of all, you need to put the
[DataContract]on every class that you want to have serialized and deserialized by WCF – it is not automatically inherited!If you’re dealing with collections of things, then you might need to check into the
CollectionDataContract:Also, WCF and SOA in general are quite a bit different from OOP and don’t handle inheritance all that well. You will most likely have to put
[ServiceKnownTypes]or[KnownType]attributes on your service contracts in places where you want to use and support polymorphism.So if you have a service method that accepts a
Parent, but should also be able to accept aChildinstance as well, then you need to decorate the method with the[KnownType]attribute to make this information available to WCF.See the MSDN Documentation on the KnownType attribute, or check out this other SO question on the topic.
Marc