views:

2053

answers:

2

I'm using .net 2.0 with NHibernate/ActiveRecord and WCF.

I haven't been using NH Lazy load so far, but the performance penalties are too big to ignore, so I'm starting to use it.

From what I've read so far, it's not an easy subject, using NH entities with lazy loading and serializing to WCF, but the benefits are too great to ignore.

Using code I've found here: WCF Serialization With NHibernate, I've been able to get WCF to recognize the basic types.

I'm also creating the DataContractSerializer like this:

public override XmlObjectSerializer CreateSerializer(Type standard, XmlDictionaryString name, XmlDictionaryString NS, IList<Type> knownTypes)
{
    return new DataContractSerializer(standard, name, NS, knownTypes, 0x989680, false, true /* Preserve References*/, new HibernateDataContractSurrogate());
}

My problem is when I have something like this:

[DataContract, ActiveRecord("POS_POSCUSTOMERS",Lazy = true)]
public class POSCustomer : ActiveRecordForPOS<POSCustomer>
{
    private Branch belongsToBranch;

    [DataMember,BelongsTo("BRANCH")]
    public virtual Branch BelongsToBranch
    {
        get { return belongsToBranch; }
        set { belongsToBranch = value; }
    }
}

and

[DataContract, ActiveRecord("BRANCHES",Lazy = true)]
public class Branch : ActiveRecordForPOS<Branch>
{
    private POSCustomer defaultPOSCustomer;

    [DataMember, BelongsTo("POS_POSCUSTNUM", Cascade= CascadeEnum.None)]
    public virtual POSCustomer DefaultPOSCustomer
    {
        get { return defaultPOSCustomer; }
        set { defaultPOSCustomer = value; }
    }
}

Branch.DefaultPOSCustomer and POSCustomer.BelongsToBranch are two unrelated entities, but they're the same entities, for example Branch 200 has DefaultPOSCustomer 100, and POSCustomer has BelongsToBranch 200.

The problem is that when WCF tries to serialize the object graph, it bounces between DefaultPOSCustomer and BelongsToBranch, as if doesn't recognize that they're the same entities and that it has already serialized them, until it runs into stack overflow.

If I turn off the Lazy = true on these classes, the serialization works just fine.

  1. How does DataContractSerializer decides that an entity has already been serialized?
  2. How can I stop this behaviour?
  3. Is there perhaps some other way to serialize lazy loading entities with WCF ?

p.s. I've been considering another solution, of creating something similar to NHibernate Proxy, but replace the properties that are related to other classes with the raw key, so instead of having a property of type Branch, I'd have a property of type int with the value 200. This way I might be able to avoid the cycle problem I've ran into, but i'd try this as a last resort, as it would be pretty complicated to maintain.

edit: I've got a lot of entities, so creating a dto for each one is out of the question, and creating it dynamically would be complicated, so i'd rather avoid that or use it as last resort. I also need to do business logic on the server side, so I'll be needing the entities rather than the raw data.

edit: well, no luck going the direct NH/AR/WCF way. I'll go with creating the DTO which seems to be easier.

A: 

It's too late to answer ;-) But anyway, this happens because lazy-loaded objects are NH proxy classes; and seems that they're different when loaded like this. One solution is to override .Equals() to compare Id property for persistent entities. I guess your entity has something like primary key. This will probably make WCF happy, only if it uses equality, not references comparision. If WCF always use ReferenceEquals - no luck.

queen3
+2  A: 

I just noticed that I didn't close my question, I went with the DTO solution and it's working just fine so far.

Miki Watts