title: Failed to serialize the response body for content type
body: <p>I'm building a MVC4 application that uses both Controllers and ApiControllers. I modified the default Web API route to include action names. When I try to get a list of Benchmarks, I'm getting this error message:</p>
  
  <blockquote>
    <p>The 'ObjectContent`1' type failed to serialize the response body for
    content type 'application/json; charset=utf-8'</p>
  </blockquote>
  
  <p>The InnerException is this (I'm returning JSON in that case, same happens with XML):</p>
  
  <pre><code>"InnerException": {
  "Message": "An error has occurred.",
  "ExceptionMessage": "Error getting value from 'IdentityEqualityComparer' on 'NHibernate.Proxy.DefaultLazyInitializer'.",
  "ExceptionType": "Newtonsoft.Json.JsonSerializationException",
  "StackTrace": " at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value) at System.Net.Http.Formatting.JsonMediaTypeFormatter.&lt;&gt;c__DisplayClassd.&lt;WriteToStreamAsync&gt;b__c() at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)",
  "InnerException": {
  "Message": "An error has occurred.",
  "ExceptionMessage": "Common Language Runtime detected an invalid program.",
  "ExceptionType": "System.InvalidProgramException",
  "StackTrace": " at GetIdentityEqualityComparer(Object ) at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
  }
  </code></pre>
  
  <p>This is the code that I run:</p>
  
  <pre><code>// GET api/benchmark/getincomplete
          [HttpGet]
          public IList&lt;Benchmark&gt; GetIncomplete()
          {
              var s = HibernateModule.CurrentSession;
              var benchList = s.QueryOver&lt;Benchmark&gt;()
                                  .Where(b =&gt; !b.Completed)
                                  .Where(b =&gt; !b.Deleted)
                                  .OrderBy(b =&gt; b.Id).Asc
                                  .List&lt;Benchmark&gt;();
  
              return benchList;
          }
  </code></pre>
  
  <p>And this is the Benchmark model:</p>
  
  <pre><code>public class Benchmark
      {
          public virtual int Id { get; set; }
          [Required]
          [DataType(DataType.Date)]
          public virtual DateTime Date { get; set; }
          [Required, ScriptIgnore]
          public virtual IList&lt;TestResult&gt; Results { get; set; }
          [Required]
          public virtual IList&lt;TestCase&gt; TestCases { get; set; }
          [AllowHtml]
          public virtual string Description { get; set; }
          public virtual Device Device { get; set; }        
          public virtual bool Published { get; set; }
          [Display(Name = "Deleted"), ScriptIgnore]
          public virtual bool Deleted { get; set; }
          public virtual bool Completed { get; set; }
  
          public Benchmark() 
          {
              Results = new List&lt;TestResult&gt;();
              TestCases = new List&lt;TestCase&gt;();
              Published = false;
              Deleted = false;
              Completed = false;
          }
      }
  </code></pre>
  
  <p>I'm not quite sure where the problem lies. Could it be the NHibernate Proxy (I use Fluent NHibernate)? The odd thing is that if I don't use an ApiController, and manually return JSON, this works just perfectly!</p>
  
  <p>Update:</p>
  
  <p>As per the answer below, this is the code I had to add in Application_Start() :</p>
  
  <pre><code>HttpConfiguration config = GlobalConfiguration.Configuration;
  ((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;
  </code></pre>