Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 354725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:57:04+00:00 2026-05-12T11:57:04+00:00

I’m using .net 2.0 with NHibernate/ActiveRecord and WCF. I haven’t been using NH Lazy

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:57:05+00:00Added an answer on May 12, 2026 at 11:57 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.