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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:07:50+00:00 2026-06-05T11:07:50+00:00

I am implementing a Sample WCF service to understand How DataContractSerializer serializes bidirectional entities.

  • 0

I am implementing a Sample WCF service to understand How DataContractSerializer serializes bidirectional entities. I have the following DataContracts.

[DataContract]
public class Process
{
    [DataMember]
    public string ProcessName { get; set; }
    [DataMember]
    public string Memory { get; set; }
    [DataMember]
    public User UserOfProcess { get; set; }
}

[DataContract]
public class User
{
    [DataMember]
    public string UserID { get; set; }
    [DataMember]
    public string UserName { get; set; }
    [DataMember]
    public List<Process> ProcessesOfUser { get; set; }
}

As you may notice that User have number of Process and every Process in turn has a User associated with it.

  [OperationContract]
  User GetUser();

When I run this method from WCFTestClient, I get an exception and that is understandable because whenever we serialize ProcessOfUser in User it in trun will serialize User again.

To avoid this, I remove the DataMember attribute from UserOfProcess in Process[DataContract]. It works fine.

My question is,
1. Is it the right way to solve this problem,or Is there some other way to this?
I also find the [IgnoreDataMember] attribute to avoid its serialization.
2. Can I add or remove the [DataMember] attribute programmatically?

  • 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-05T11:07:53+00:00Added an answer on June 5, 2026 at 11:07 am

    You can use the [IsReference] attribute to tell WCF to resolve circular dependencies.

    However, after using this myself on business objects, I ran into too many complications and ended up using explicit DTOs instead, and effectively doing what you’ve done and omit the circular reference. I wire up the references again at the client end when the DTO is transformed back to a business object.

    IsReference should be fine for simple object graphs though.

    EDIT: Regarding DTO’s

    As I said I originally serialised domain objects across the wire but it got way too messy in the end.

    Firstly because I also serialise those same objects for persistence, so I ended up with conflicting serialisation needs, but mainly because DataContractSerializer does not call constructors so you have no guarantee that your object is valid. I ended up having large [OnDeserialized] methods to make sure members weren’t null etc. Doing this on collection members caused MASSIVE headaches due to the way WCF serialises object graphs.

    Using DTO’s makes it all go away and exposes a really clean object showing exactly what’s going over the service. You get a DTO and then can do your mapping knowing everything is in place and not in some wierd semi-serialised state. If your domain objects are really simple you might get away with it, but I personally wouldn’t recommend it after the pain is caused me.

    The DTO’s are stupidly simple:

    [DataContract]
    public class UserProcessesDTO
    {
        [DataMember] public string       UserID          { get; set; }
        [DataMember] public string       UserName        { get; set; }
        [DataMember] public ProcessDTO[] Processes       { get; set; }
    }
    
    [DataContract]
    public class ProcessDTO
    {
        [DataMember] public string       ProcessName     { get; set; }
        [DataMember] public string       Memory          { get; set; }
    }
    

    All my collections now go over the wire as simple arrays. Dictionaries etc in business objects get cleanly built at the client end rather than trying to serialise them.

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

Sidebar

Related Questions

When Following the CoreDataBooks sample code and implementing it into my app, I have
I'm implementing a simple REST service with the WCF Web API and attempt to
I am implementing a Silverlight application with a WCF RIA service in the server
I am implementing wcf web service hosting in IIS with basicHttpBinding those should be
Would the following be a viable strategy for implementing versioning(using example as a sample
I have a WCF service which exposes a Generic interface (and the service has
I have build a WCF template service which talks (original: discuss) with my EF
I am following sample code on implementing MVVM in Silverlight (see: http://msdn.microsoft.com/en-us/magazine/dd458800.aspx ). On
I have been looking into refactoring some old code into a new WCF service,
I'm implementing reconnection mechanism for WCF service proxy (NetTcpBinding) in Silverlight . Operations of

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.