title: MongoDB .FindSync() result can not be serialized
body: <p>Tried to build a simple WebAPI with MongoDB, returns a Patient document based on value of _id.</p>
  
  <p>Definition of Patient, <strong>Patient.cs</strong></p>
  
  <pre><code>namespace PatientData.Models
  {
      public class Patient
      {
          [BsonElement("_id")]        
          [BsonRepresentation(BsonType.ObjectId)]     
          public string  Id { get; set; }
  
          public string  Name { get; set; }                   
          public ICollection&lt;Medication&gt; Medications { get; set; }    
      }
      public class Medication 
      {
          public string Name { get; set; }
          public int Doses { get; set; }
      }
  }
  </code></pre>
  
  <p>Database mongodb access, <strong>PatientDB.cs</strong>:</p>
  
  <pre><code>namespace PatientData.Models
  {
      public static class PatientDB
      {
          static MongoClient client = new MongoClient("mongodb://localhost");
          static IMongoDatabase db = client.GetDatabase("Patients");
  
      public static IMongoCollection&lt;Patient&gt; Open()  
      {
          return db.GetCollection&lt;Patient&gt;("Patients");       
      }
  
      public static IQueryable&lt;Patient&gt; query()
      {
          return db.GetCollection&lt;Patient&gt;("Patients").AsQueryable&lt;Patient&gt;();       // .AsQueryable() is still availabe in driver version 2.# as an extension for collection. so .Any() is still available as well.
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>API <strong>controller</strong>:</p>
  
  <pre><code>namespace PatientData.Controllers
  {
  
  public class PatientsController : ApiController
  {
      IMongoCollection&lt;Patient&gt; _patients;
  
      public PatientsController()
      {
          _patients = PatientDB.Open();
      }
      public IEnumerable&lt;Patient&gt; Get()
      {
          return _patients.Find&lt;Patient&gt;(_=&gt;true).ToList();   
      }
  
      public HttpResponseMessage Get(string id)   
      {
          var theFilter = Builders&lt;Patient&gt;.Filter.Eq("Id", id);
          var thePatient = _patients.FindSync&lt;Patient&gt;(theFilter);      
  
          //return (Patient)thePatient;         
  
          return Request.CreateResponse(thePatient);
  
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Compiled ok, got run-time exception, for example, URL</p>
  
  <blockquote>
    <p><a href="http://localhost:49270/api/Patients/5768a6f48200fa07289c93e8" rel="nofollow">http://localhost:49270/api/Patients/5768a6f48200fa07289c93e8</a>
    Type
    'MongoDB.Driver.Core.Operations.AsyncCursor`1[PatientData.Models.Patient]'
    cannot be serialized. Consider marking it with the
    DataContractAttribute attribute, and marking all of its members you
    want serialized with the DataMemberAttribute attribute. If the type is
    a collection, consider marking it with the
    CollectionDataContractAttribute. See the Microsoft .NET Framework
    documentation for other supported types.</p>
  </blockquote>
  
  <p>If returned type is "Patient" not "HttpResponseMessage", and use </p>
  
  <pre><code>return (Patient)thePatient;
  </code></pre>
  
  <p>run-time exception is more interesting:</p>
  
  <blockquote>
    <p>'System.InvalidCastException
    Unable to cast object of type 'MongoDB.Driver.Core.Operations.AsyncCursor`1[PatientData.Models.Patient]' to type 'PatientData.Models.Patient'.</p>
  </blockquote>
  
  <p>If the cursor is typed as <em>PatientData.Models.Patient</em>, why can't be that type? </p>
  
  <p>This is based on Scott Allen's <a href="http://pluralsight.com/training/Player?author=scott-allen&amp;name=aspdotnet-mvc5-fundamentals-m1-introduction&amp;mode=live&amp;clip=0&amp;course=aspdotnet-mvc5-fundamentals" rel="nofollow">ASP.NET MVC 5 Fundamentals</a>, WebAPI 2, Query by ID. He's using 1.x driver</p>
  
  <p>Mine is:</p>
  
  <ul>
  <li>mongo server 3.2, 64-bit </li>
  <li>mongo C# driver 2.2.4</li>
  </ul>