title: What attribute to use for serializing collections in WCF?
body: <p>I have a class with a generic collection as a property that I'm serializing with WCF:</p>
  
  <pre><code>[ServiceContract]
  public interface IService1
  {
      [OperationContract]
      TestObject Load();
  }
  
  [DataContract]
  public class TestObject
  {
      [DataMember]
      public Collection&lt;object&gt; Properties = new Collection&lt;object&gt;();
  }
  </code></pre>
  
  <p>It works fine until I add another collection as an item in the collection (indicated by the "bad line" comment)</p>
  
  <pre><code>public class Service1 : IService1
  {
      public TestObject Load()
      {
          var obj = new TestObject();
  
          // bad line
          obj.Properties.Add(new Collection&lt;object&gt;());
  
          return obj;
      }
  }
  </code></pre>
  
  <p>at which I get the exception:</p>
  
  <p>System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. </p>
  
  <p>If I remove the TestObject class and just return a Collection, then adding another collection to the collection strangely enough works fine:</p>
  
  <pre><code>[ServiceContract]
  public interface IService1
  {
      [OperationContract]
      Collection&lt;object&gt; Load();
  }
  
  public class Service1 : IService1
  {
      public Collection&lt;object&gt; Load()
      {
          Collection&lt;object&gt; coll = new Collection&lt;object&gt;();
  
          coll.Add(DateTime.Now);
          coll.Add(new Collection&lt;object&gt;() { DateTime.Now, new Collection&lt;object&gt;() });
          coll.Add("sdf");
          coll.Add(99);
  
          return coll;
      }
  }
  </code></pre>
  
  <p>I've tried replacing the Collection with a List or an ArrayList. I've tried creating my own derived class from Collection. I've tried using combinations of DataContract, KnownType, ServiceKnownType and CollectionDataContract attributes, but I'm either using them incorrectly or I have another problem.</p>
  
  <p>So is there a way to add collections to my collection in the first example by either applying an attribute or using a derived class? Or is there any way to do this at all?</p>