Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF serialization
example:
[DataContract]
public class TestObject
{
    [DataMember]
    public string Member1 { get; set; }
 
    [DataMember]
    public int Member2 { get; set; }
 
}
 
[DataContract]
public class TestClass
{
    [DataMember]
    public TestObject MyObject1 { get; set; }
 
    [DataMember]
    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 13:32pm
impeham724
Edited 5-Oct-12 19:17pm
v6

1 solution

Rate this: bad
good
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.
 
[EDIT]
 
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:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.preserveobjectreferences.aspx[^].
 
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.
 
[END EDIT]
 
—SA
  Permalink  
v3
Comments
impeham at 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.
Sergey Alexandrovich Kryukov at 5-Oct-12 21:45pm
   
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.
--SA
Sergey Alexandrovich Kryukov at 5-Oct-12 21:50pm
   
Anyway, please see the updated answer, after [EDIT]. You really explain your problem in a very cryptic way. Try to explain it clearly.
--SA
impeham at 5-Oct-12 22:26pm
   
I believe this attribute is what i was looking for - thanks :)
Sergey Alexandrovich Kryukov at 6-Oct-12 1:01am
   
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.
--SA
impeham at 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...
Sergey Alexandrovich Kryukov at 6-Oct-12 0:58am
   
It won't if you follow my advice written after [EDIT].
--SA

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

  Print Answers RSS
0 Mathew Soji 309
1 BillWoodruff 300
2 Afzaal Ahmad Zeeshan 252
3 DamithSL 225
4 Maciej Los 215
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2014
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