Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
example:
C#
[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
Updated 5-Oct-12 18:17pm
v6

1 solution

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
 
Share this answer
 
v3
Comments
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.
Sergey Alexandrovich Kryukov 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 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 5-Oct-12 22:26pm    
I believe this attribute is what i was looking for - thanks :)
Sergey Alexandrovich Kryukov 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900