DataContractSerializer每个请求多次序列化同一个对象

时间:2009-04-26 13:36:19

标签: wcf datacontractserializer

我正在编写一个将由Silverlight应用程序使用的WCF应用程序。我完成了大部分的设计工作,现在正在实施,这让我想出了这个问题。

以下是我的应用程序中存在的一些示例:

[DataContract]
class Person
{
    [DataMember]
    private Towel mostRecentlyUsedTowel;

    [DataMember]
    private Gym gym; //the gym that this person attends

    ...
}

[DataContract]
class Gym
{
    [DataMember]
    private List<Towel> towels; //all the towels this gym owns

    ...
}

这就是我所得到的:在我的应用程序中,mostRecentlyUsedTowel将指向该人体育馆毛巾列表中的某些内容。我的一些请求会序列化一个Person对象。

DataContractSerializer是否足够聪明,可以注意到它被要求两次序列化对象的完全相同的实例?如果是这样,它是如何处理的?

如果它只是将同一个实例序列化两次,我应该如何处理这个问题,所以我不会通过链接发送不必要的数据?

1 个答案:

答案 0 :(得分:7)

以下代码:

[TestMethod]
public void CanSerializePerson()
{
    var towel1 = new Towel() { Id = 1 };
    var towel2 = new Towel() { Id = 2 };
    var towel3 = new Towel() { Id = 3 };
    var gym = new Gym();
    gym.towels.Add(towel1);
    gym.towels.Add(towel2);
    gym.towels.Add(towel3);

    var person = new Person()
    {
      recentlyUsedTowel = towel1,
      gym = gym
    };

    var sb = new StringBuilder();
    using (var writer = XmlWriter.Create(sb))
    {
        var ser = new DataContractSerializer(typeof (Person));
        ser.WriteObject(writer, person);
    }

    throw new Exception(sb.ToString());
}

public class Person
{
    public Towel recentlyUsedTowel { get; set; }
    public Gym gym { get; set; }
}

public class Gym
{
    public Gym()
    {
        towels = new List<Towel>();
    }

    public List<Towel> towels { get; set; }
}


public class Towel
{
    public int Id { get; set; }
}

将评估为:

<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org">
  <gym>
    <towels>
      <Towel><Id>1</Id></Towel>
      <Towel><Id>2</Id></Towel>
      <Towel><Id>3</Id></Towel>
    </towels>
  </gym>
  <recentlyUsedTowel><Id>1</Id></recentlyUsedTowel>
</Person>

如果您将IsReference属性添加到Towel类的DataContract属性,如下所示:

[DataContract(IsReference=true)]
public class Towel
{
    // you have to specify a [DataMember] in this because you are 
    // explicitly adding DataContract
    [DataMember]
    public int Id { get; set; }
}

你会得到这样的输出:

<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org">
  <gym>
    <towels>
      <Towel z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
        <Id>1</Id>
      </Towel>
      <Towel z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
        <Id>2</Id>
      </Towel>
      <Towel z:Id="i3" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
        <Id>3</Id>
      </Towel>
    </towels>
  </gym>
  <recentlyUsedTowel z:Ref="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" />
</Person>

希望这有助于。