Reputation: 15069
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.
Upvotes: 1
Views: 2770
Reputation: 754258
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!
[DataContract]
class Parent
{
.....
}
[DataContract]
class Child : Parent
{
.....
}
If you're dealing with collections of things, then you might need to check into the CollectionDataContract
:
[CollectionDataContract]
[KnownType(typeof(Parent))]
[KnownType(typeof(Child))]
public class CustomCollection : List<Parent>
{
}
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 a Child
instance 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
Upvotes: 1
Reputation: 3099
I would recommend adding IsReference and KnownType to your classes, like shown below:
[DataContract(IsReference = true)]
[KnownType(typeof(Child))]
class Parent
{
[DataMember]
some data members
[DataMember]
Child myChild;
}
[DataContract(IsReference = true)]
class Child : Parent
{
[DataMember]
some more data members
[DataMember]
Parent myParent;
}
Upvotes: 0