tags:

views:

316

answers:

2

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 Parents ?

(I get strange results, sometimes the channel faults, sometime it doesn't fault but give me no data, until I remove all the children from the list.

Thanks, Dani

+1  A: 

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

marc_s
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
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
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
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
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
A: 

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
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
For collection of generic objects that work through WCF read here: http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html
Boris Modylevsky