title: Circular references preventing serialization of object graph
body: <p>I've got a simple data model involving Weeds and Weed Families.  </p>
  
  <p><code>WeedFamily &lt;-1---*-&gt; Weed</code> (WeedFamily and Weed have a one-to-many relationship)</p>
  
  <p>I'm attempting to complete my first ApiController so that I can easily retrieve my data as JSON for an AngularJS application.  When I access the <code>/WeedAPI/</code> URL in my application, I get the following error.  I'm pretty sure the problem is that I have circular references between <code>Weed</code> and <code>WeedFamily</code>.</p>
  
  <p>How should I change my data model so that the JSON serialization will work while maintaining the bi-directional quality of the <code>Weed</code>-<code>WeedFamily</code> relationship?</p>
  
  <p>(ie. I want to still be able to build expressions like the following:</p>
  
  <pre><code> WeedData.GetFamilies()["mustard"].Weeds.Count
  </code></pre>
  
  <p>and </p>
  
  <pre><code>WeedData.GetWeeds()[3].Family.Weeds
  </code></pre>
  
  <p>) </p>
  
  <p>The error:</p>
  
  <pre><code>&lt;Error&gt;
      &lt;Message&gt;An error has occurred.&lt;/Message&gt;
      &lt;ExceptionMessage&gt;
          The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
      &lt;/ExceptionMessage&gt;
      &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
      &lt;StackTrace/&gt;
      &lt;InnerException&gt;
          &lt;Message&gt;An error has occurred.&lt;/Message&gt;
          &lt;ExceptionMessage&gt;
              Object graph for type 'WeedCards.Models.WeedFamily' contains cycles and cannot be serialized if reference tracking is disabled.
          &lt;/ExceptionMessage&gt;
          &lt;ExceptionType&gt;
              System.Runtime.Serialization.SerializationException
          &lt;/ExceptionType&gt;
          &lt;StackTrace&gt;
              at System.Runtime.Serialization.XmlObjectSerializerWriteContext.OnHandleReference(XmlWriterDelegator xmlWriter, Object obj, Boolean canContainCyclicReference) at WriteWeedToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract ) at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDecl...etc
          &lt;/StackTrace&gt;
      &lt;/InnerException&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>My data:</p>
  
  <pre><code>public class WeedData
  {
      public static Dictionary&lt;string,WeedFamily&gt; GetFamilies(){
          return new Dictionary&lt;string,WeedFamily&gt;
          {
               {"mustard",new WeedFamily("Mustard","Brassicaceae")}
              ,{"pigweed",new WeedFamily("Pigweed","Amaranthus")}
              ,{"sunflower",new WeedFamily("Sunflower","Asteraceae")}
          };
      }
  
      public static List&lt;Weed&gt; GetWeeds(){
          var Families = GetFamilies();
          return new List&lt;Weed&gt;
          {
               new Weed("Hairy Bittercress","Cardamine hirsuta",Families["mustard"])
              ,new Weed("Little Bittercress","Cardamine oligosperma",Families["mustard"])
              ,new Weed("Shepherd's-Purse","Capsella bursa-pastoris",Families["mustard"])
              ,new Weed("Wild Mustard","Sinapis arvensis / Brassica kaber",Families["mustard"])
              ,new Weed("Wild Radish","Raphanus raphanistrum",Families["mustard"])
              ,new Weed("Radish","Raphanus sativus",Families["mustard"])
              ,new Weed("Redroot Pigweed","Amaranthus retroflexus",Families["pigweed"])
              ,new Weed("Prickly Lettuce","Lactuca serriola",Families["sunflower"])
              ,new Weed("Spiny Sowthistle","Sonchus asper",Families["sunflower"])
              ,new Weed("Annual Sowthistle","Sonchus oleraceus",Families["sunflower"])
  
          };
      }
  }
  </code></pre>
  
  <p>My model classes:</p>
  
  <pre><code>[Serializable]
  public class Weed
  {
      public string CommonName;
      public string LatinName;
      public List&lt;WeedPhoto&gt; Photos;
      public WeedFamily Family;
  
      public Weed(string commonName, string latinName)
      {
          CommonName = commonName;
          LatinName = latinName;
      }
  
      public Weed(string commonName, string latinName, WeedFamily family)
      {
          CommonName = commonName;
          LatinName = latinName;
          Family = family;
          Family.Weeds.Add(this);
      }
  
      override public string ToString()
      {
          return CommonName + " (" + LatinName + ")";
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>[Serializable]
  public class WeedFamily
  {
      public string CommonName;
      public string LatinName;
      public List&lt;Weed&gt; Weeds;
  
      public WeedFamily(string commonName, string latinName)
      {
          CommonName = commonName;
          LatinName = latinName;
          Weeds = new List&lt;Weed&gt;();
      }
  }
  </code></pre>
  
  <p>Finally, the ApiController:</p>
  
  <pre><code>public class WeedAPIController : ApiController
  {
      //
      // GET: /WeedAPI/
  
      public IEnumerable&lt;Weed&gt; GetAllWeeds()
      {
          return WeedData.GetWeeds();
      }
  
  }
  </code></pre>