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 9074765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:42:51+00:00 2026-06-16T18:42:51+00:00

In a WCF service, I have a situation where there is a datacontract (object)

  • 0

In a WCF service, I have a situation where there is a datacontract (object) that contains a collection of child items (where the collection has a datamemeber attribute). The objects in the child collection have a reference to the parent as a property. If the parent object child collection is empty or contains a null parent reference, all is good, but if the parent reference is populated and parent children are populated, it serializes for ever.

Here’s a set of test console code to show you what I’m talking about.

class Program
    {
        static void Main(string[] args)
        {

            MyParentObject parent = BuildParent();

            string xml = Serializer<MyParentObject>.Serialize(parent);

            System.Console.Write(xml);

            System.Console.ReadKey();

        }

        static MyParentObject BuildParent()
        {

            MyParentObject parent = new MyParentObject();
            parent.MyParentObjectId = 123;
            parent.MyParentObjectName = "Test Parent Object";
            parent.MyChildren = new List<MyChildObject>();

            for (int i = 0; i < 10; i++)
            {
                MyChildObject child = new MyChildObject();
                child.MyParent = parent;
                child.MyChildObjectId = i;
                child.MyChildObjectName = string.Format("Test Child Name {0}", i.ToString());
                parent.MyChildren.Add(child);
            }

            return parent;

        }

    }


    [DataContract]
    public class MyParentObject
    {

        [DataMember]
        public int MyParentObjectId { get; set; }

        [DataMember]
        public string MyParentObjectName { get; set; }

        [DataMember]
        public List<MyChildObject> MyChildren { get; set; }

    }

    [DataContract]
    public class MyChildObject
    {
        [DataMember]
        public MyParentObject MyParent { get; set; }

        [DataMember]
        public int MyChildObjectId { get; set; }

        [DataMember]
        public string MyChildObjectName { get; set; }

    }

public class Serializer<T>
    {

        public static string Serialize(T entity)
        {

            StringBuilder sb = new StringBuilder();


            DataContractSerializer dcs = new DataContractSerializer(typeof(T));

            using (XmlWriter writer = XmlWriter.Create(sb))
            {

                dcs.WriteObject(writer, entity);
                writer.Flush();
            }

            return sb.ToString();
        }

    }

Other than clearing out child/parent references that cause this infinite serialization loop, are there any ways around this?

EDIT: I know I can just remove the datamember attribute, but I’d like to keep it, and simply not serialize infinitely.

  • 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-06-16T18:42:52+00:00Added an answer on June 16, 2026 at 6:42 pm

    Try using the IsReference property in your DataContract attribute

    [DataContract(IsReference = true)] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I am running in a WCF service that has
I have Asp.Net solution that contains few WCF Service projects. Every time i run
Short Background: Currently my situation is such that I have a service (WCF) of
Here is my situation. I have written a WCF service which calls into one
Hi I want to create a WCF service that have login method, which is
We have a service reference that points at a WCF service, this acts as
Suppose I had a WCF service that I have coded up, like Clemens Vasters's
Background: I have a WCF service deployed on my local machine that in turns
The current situation is as follows: We have an production .net 3.5 WCF service,
We currently have a situation where we have a WCF service which generates custom

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.