tags:

views:

153

answers:

1

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?

+1  A: 

The PersistentGenericSet is part of 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.

Keith Bloom
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
I've found a solution (and learnt something about NHibernate), please see the edited answer.
Keith Bloom
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
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