0

I'm trying to send objects retrieved by NHibernate over WCF, but whenever a have a property of ICollection I get an exception.

When NHibernate gets the data from the database this property is intitialized with an instance of PersistentGenericSet.

Is there a way I can send a PersistentGenericSet over WCF?

-or-

Is there some way making NHibernate initialize these properties with another type?

Ian Oakes
  • 10,153
  • 6
  • 36
  • 47
  • 100x duplicate: http://stackoverflow.com/questions/1958684/nhibernate-how-do-i-xmlserialize-an-isett – Mauricio Scheffer Mar 31 '10 at 12:36
  • So many duplicates, but no real answer apart from this is a bad idea use dto's etc. If it can't be done then maybe NH is not a good fit to requirements of this particular project, one set of classes to pass data between server and UI. So I guess the answer I'm looking for, it it possible and if so how? – Ian Oakes Mar 31 '10 at 13:01

1 Answers1

1

The PersistentGenericSet is part of NHibernate (used to track changes in collections). It is based on the ISet interface and classes from Iesi.Collections, which was used to fill a gap in the .Net framework as there isn't a Set type. I guess that WCF has a problem serializing this type.

A quick fix is to change your NHibernate mappings to use a Bag instead of a Set. Then you can use a normal IList<T> instead of Set<T> in your classes w.

A better solution is to create a remote facade which sends DTOs to your WCF endpoints. This will allow you to keep the interface of your internal types separate from those exposed as remote services. Jimmy Bogards Automapper is a great tool which will help with the mapping process.

Edit

After re-reading the problem I had a look around the and came across this article which describes a workaround for sending NHibernate collections over WCF. David Brion has written a good follow up article.

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
Keith Bloom
  • 2,391
  • 3
  • 18
  • 30
  • Tried replacing set with bag in the mapping file, but now I get a PersistentGenericBag instead, what I would like is for NH to set my collection with a POCO collection which can be serialized. The closest solution I've found so far is to use the attribute access="field.camelcase-underscore", and then have the property return ToList() – Ian Oakes Mar 31 '10 at 11:19
  • I've found a solution (and learnt something about NHibernate), please see the edited answer. – Keith Bloom Mar 31 '10 at 12:57
  • I've seen a couple of solutions like this, but it seems to imply that you need to have NHibernate references on the client side of the WCF call to be able to deserialize the NHibernate collections. – Ian Oakes Mar 31 '10 at 13:09
  • Yes, that is true. At some point though you will have to turn those Nhibernate collections in to a normal collection that WCF will happily serialize or extend the way WCF does serialization somehow. This may help http://stackoverflow.com/questions/967523/wcf-custom-serializer – Keith Bloom Mar 31 '10 at 13:22