1

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.

Kit
  • 20,354
  • 4
  • 60
  • 103
Dani
  • 14,639
  • 11
  • 62
  • 110

2 Answers2

1

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

Community
  • 1
  • 1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It seems that you are right.. but, although I've added all the right attributes I still get some strange behavior. I think it's involves the face that the object are being created by NHibernate, and maybe the problem is there somewhere... – Dani Oct 04 '09 at 12:22
  • Dani, now you mention, that you use NHibernate As far as I remember it uses classes with protected setters, am I right? While WCF requires that all the properties marked with DataContract attribute have public getter and setter – Boris Modylevsky Oct 04 '09 at 15:00
  • 1
    Hi, WCF is fine with the NH properties, While still investigating, I've found the problem was that I used a criteria typeof(father) to get the objects, and NH returned all the father and sons (as expected...) and WCF couldn't handle it - b/c it expected only objects of type parent (although containing the child object inside was not a problem - having both parent and child on the array returned faulted the channel. Now I'm checking if I can change the query to get only "Father" objects and it should resolve the issue, I hope. – Dani Oct 05 '09 at 10:14
  • Marc, it looks like another problem might be masking this issue. There is a circulare reference issue here, the parent points the child that points the parent. NH is ok with this, but WCF is not. I have another Q. about this here, and I'm still investigating many solutions for this problem, once the circular reference will be resolve, I can get back to the inheritance issue... – Dani Oct 14 '09 at 14:19
  • resolved the circular reference issue, I'll add back the inheritance issue and run a test with your solution to see if it works. – Dani Oct 27 '09 at 16:02
0

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;
}
Boris Modylevsky
  • 3,029
  • 1
  • 26
  • 42
  • Tried it, and also killed the inheritance, but it still doesn't work (now the father only has a IList of it's children but the child object doesn't inherit the father (they both inherit some base class) – Dani Oct 04 '09 at 13:33
  • For collection of generic objects that work through WCF read here: http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html – Boris Modylevsky Oct 04 '09 at 15:07