一个对象具有多个成员的对象的WCF序列化 [英] WCF serialization for an object with more than one member with same object

查看:84
本文介绍了一个对象具有多个成员的对象的WCF序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

[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;


问题是-如果我将测试"对象发送到wcf服务的方法,那么Member1和Member2的序列化是否会加倍(即使Member1和Member2是同一对象,数据也会被序列化)? br/>
如果是这种情况,是否有办法避免这种情况(将MyObject1中的数据序列化一次,并将MyObject2设置为指向服务端中的同一对象)?


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)?

推荐答案

您永远不需要避免任何事情.您的Member1Member2仍然是不同的对象,一旦在合同中包含它们,就应该对其进行序列化.如果出于某些奇怪的原因它们具有相同的目的,请不要对其中之一使用[DataMember]属性.

附带建议:还对所有[DataContract]属性使用名称空间URI-它可以使您的数据模型基于XML的数据格式世界独一无二.



请在下面查看我的评论.如果我的猜测是正确的,那么如果您阅读System.Runtime.Serialization.DataContractSerializer.PreserveObjectReferences属性,或者只是简单地尝试一下,则应该了解所有内容:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.preserveobjectreferences.aspx [ ^ ].

你有主意吗?特别是,这是一个非常重要的功能,因为您要保留不是树的图形.如果是这种情况,但是您无法对此属性使用true值,则可能会导致无限递归,从而导致堆栈溢出.

同样,要了解XML格式的工作原理,请尝试一下.它使用对ID的特殊引用.

[END EDIT]

—SA
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:
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


这篇关于一个对象具有多个成员的对象的WCF序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆