Click here to Skip to main content
13,048,805 members (84,990 online)
Rate this:
Please Sign up or sign in to vote.
See more:
public class TestObject
    public string Member1 { get; set; }
    public int Member2 { get; set; }
public class TestClass
    public TestObject MyObject1 { get; set; }
    public TestObject MyObject2 { get; set; }
TestClass test = new TestClass();
test.MyObject1 = new TestObject();
test.MyObject1.Member1 = "First";
test.MyObject1.Member2 = 100;
test.MyObject2 = test.MyObject1;

The question is - if i send "test" object to a wcf service's method, will the serialization of Member1 and Member2 be doubled (the data will be serialized both for Member1 and Member2 even though they are the same object)?

If this is the case, is there a way to avoid this (serialize the data in MyObject1 once and set MyObject2 to point to the same object in the service side)?
Posted 5-Oct-12 12:32pm
Updated 5-Oct-12 18:17pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You don't need to avoid anything, ever. Your Member1 and Member2 are still different object and should be serialized, as soon as you have them in the contract. If they serve the same purpose by some weird reason, don't use the [DataMember] attribute for one of them.

A side advice: also use namespace URI for all [DataContract] attributes — it can make your data model's XML-based data format world-unique.


Please see my comment below. If my guess is correct, you should understand everything, if you read on the System.Runtime.Serialization.DataContractSerializer.PreserveObjectReferences property or simply try it out:[^].

Are you getting the idea? In particular, this is a very important feature is you want to persists the graph which are not trees. If this is the case, but you fail to use true value for this property, it may cause infinite recursion and, hence, stack overflow.

Again, to understand how it works in terms of XML format, try it out. It uses special references to IDs.


impeham 5-Oct-12 19:54pm
Well, i cannot remove the data member attribute since i need to pass both and they might also hold different objects. The reason i'm asking this is that the objects might contain large data in the form of byte[] array and i do not want this data to be passed twice if both objects are pointing to the same object that holds that data.
impeham 5-Oct-12 21:08pm
anyway - test.MyObject1 and test.MyObject2 are pointing to the same object - changing one's "Member2"'s value will be reflected in the second, so there shouldn't be a reason to serialize this data twice...
Thank you need to keep it and really need to persist it data in the contract. Where is twice? Perhaps you have different objects referencing same object (diamond-like, not to mix up with inheritance). But this is a different story.
Anyway, please see the updated answer, after [EDIT]. You really explain your problem in a very cryptic way. Try to explain it clearly.
impeham 5-Oct-12 22:26pm
I believe this attribute is what i was looking for - thanks :)
It won't if you follow my advice written after [EDIT].
Sorry, I thought you did not notice my update.
This is the serializer's property, not attribute -- please try it to make sure.
When (and if) you see it resolves the issue, please up-vote the answer (green button).
If not, I'm ready to discuss it further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100