title: DataContract serialization of circular references works too deep for the first item of collection and too finely for the rest
body: <p>I have two classes which refer on each other:</p>
  
  <pre><code>[DataContract(IsReference = true)]
  public class Student
  {
      [DataMember]
      public int Id { get; set; }
  
      [DataMember]
      public string Name { get; set; }
  
      [DataMember]
      public string Surname { get; set; }
  
      [DataMember]
      public List&lt;Course&gt; Courses { get; set; }
  
      public Student()
      {
          Courses = new List&lt;Course&gt;();
      }
  }
  
  
  [DataContract(IsReference = true)]
  public class Course
  {
      [DataMember]
      public int Id { get; set; }
  
      [DataMember]
      public string Name { get; set; }
  
      [DataMember]
      public List&lt;Student&gt; Students { get; set; }
  
      public Course()
      {
          Students = new List&lt;Student&gt;();
      }
  }
  </code></pre>
  
  <p>I want to serialize 4 Student entities, each of them has some courses attached.  after serialization only the first Student entity appears not empty. What is more, it has too many reference levels. And all others Student entities are empty:</p>
  
  <pre><code>&lt;GetStudentsResult         
  xmlns:a="http://schemas.datacontract.org/2004/07/WcfService2" 
  xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;a:Student xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" 
  z:Id="i1"&gt;
  &lt;a:Courses&gt;
  &lt;a:Course z:Id="i2"&gt;
  &lt;a:Id&gt;1&lt;/a:Id&gt;
  &lt;a:Name&gt;Operation Systems&lt;/a:Name&gt;
  &lt;a:Students&gt;
  &lt;a:Student z:Ref="i1"/&gt;
  &lt;a:Student z:Id="i3"&gt;
  &lt;a:Courses&gt;
  &lt;a:Course z:Ref="i2"/&gt;
  &lt;a:Course z:Id="i4"&gt;
  &lt;a:Id&gt;2&lt;/a:Id&gt;
  &lt;a:Name&gt;Algorithmes and data structures&lt;/a:Name&gt;
  &lt;a:Students&gt;
  &lt;a:Student z:Ref="i3"/&gt;
  &lt;a:Student z:Id="i5"&gt;
  &lt;a:Courses&gt;
  &lt;a:Course z:Ref="i4"/&gt;
  &lt;a:Course z:Id="i6"&gt;
  &lt;a:Id&gt;3&lt;/a:Id&gt;
  &lt;a:Name&gt;Basics of HTML and CSS&lt;/a:Name&gt;
  &lt;a:Students&gt;
  &lt;a:Student z:Ref="i1"/&gt;
  &lt;a:Student z:Id="i7"&gt;
  &lt;a:Courses&gt;
  &lt;a:Course z:Ref="i2"/&gt;
  &lt;a:Course z:Ref="i6"/&gt;
  &lt;/a:Courses&gt;
  &lt;a:Id&gt;3&lt;/a:Id&gt;
  &lt;a:Name&gt;Oleg&lt;/a:Name&gt;
  &lt;a:Surname&gt;Kuznetsov&lt;/a:Surname&gt;
  &lt;/a:Student&gt;
  &lt;a:Student z:Ref="i5"/&gt;
  &lt;/a:Students&gt;
  &lt;/a:Course&gt;
  &lt;/a:Courses&gt;
  &lt;a:Id&gt;4&lt;/a:Id&gt;
  &lt;a:Name&gt;Olga&lt;/a:Name&gt;
  &lt;a:Surname&gt;Petrova&lt;/a:Surname&gt;
  &lt;/a:Student&gt;
  &lt;/a:Students&gt;
  &lt;/a:Course&gt;
  &lt;/a:Courses&gt;
  &lt;a:Id&gt;2&lt;/a:Id&gt;
  &lt;a:Name&gt;Maria&lt;/a:Name&gt;
  &lt;a:Surname&gt;Vasilyeva&lt;/a:Surname&gt;
  &lt;/a:Student&gt;
  &lt;a:Student z:Ref="i7"/&gt;
  &lt;/a:Students&gt;
  &lt;/a:Course&gt;
  &lt;a:Course z:Ref="i6"/&gt;
  &lt;/a:Courses&gt;
  &lt;a:Id&gt;1&lt;/a:Id&gt;
  &lt;a:Name&gt;Egor&lt;/a:Name&gt;
  &lt;a:Surname&gt;Ivanov&lt;/a:Surname&gt;
  &lt;/a:Student&gt;
  &lt;a:Student xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
  z:Ref="i3"/&gt;
  &lt;a:Student xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"     
  z:Ref="i7"/&gt;
  &lt;a:Student xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"     
  z:Ref="i5"/&gt;
  &lt;/GetStudentsResult&gt;
  </code></pre>
  
  <p>Here is my code, filling Student and Course collections:</p>
  
  <pre><code>Student s1 = new Student { Id = 1, Name = "Egor", Surname = "Ivanov" };
          Student s2 = new Student { Id = 2, Name = "Maria", Surname = "Vasilyeva" };
          Student s3 = new Student { Id = 3, Name = "Oleg", Surname = "Kuznetsov" };
          Student s4 = new Student { Id = 4, Name = "Olga", Surname = "Petrova" };
  
          context.Students.Add(s1);
          context.Students.Add(s2);
          context.Students.Add(s3);
          context.Students.Add(s4);
  
          Course c1 = new Course
          {
              Id = 1,
              Name = "Operation Systems",
              Students = new List&lt;Student&gt;() { s1, s2, s3 }
          };
          Course c2 = new Course
          {
              Id = 2,
              Name = "Algorithmes and data structures",
              Students = new List&lt;Student&gt;() { s2, s4 }
          };
          Course c3 = new Course
          {
              Id = 3,
              Name = "Basics of HTML and CSS",
              Students = new List&lt;Student&gt;() { s3, s4, s1 }
          };
  
          context.Courses.Add(c1);
          context.Courses.Add(c2);
          context.Courses.Add(c3);
  </code></pre>
  
  <p>Please advise.</p>