我建立一个使用两个控制器和一个ApiControllers应用MVC4。 我修改了默认的Web API路线包括操作名称。 当我试图让基准的名单,我收到此错误信息:
在“ObjectContent`1”型未能序列化反应体内容类型“应用/ JSON; 字符集= UTF-8'
设置InnerException是这样(我在这种情况下返回JSON,同样使用XML发生):
"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.<>c__DisplayClassd.<WriteToStreamAsync>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)"
}
这是我运行的代码:
// GET api/benchmark/getincomplete
[HttpGet]
public IList<Benchmark> GetIncomplete()
{
var s = HibernateModule.CurrentSession;
var benchList = s.QueryOver<Benchmark>()
.Where(b => !b.Completed)
.Where(b => !b.Deleted)
.OrderBy(b => b.Id).Asc
.List<Benchmark>();
return benchList;
}
这是基准模型:
public class Benchmark
{
public virtual int Id { get; set; }
[Required]
[DataType(DataType.Date)]
public virtual DateTime Date { get; set; }
[Required, ScriptIgnore]
public virtual IList<TestResult> Results { get; set; }
[Required]
public virtual IList<TestCase> 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<TestResult>();
TestCases = new List<TestCase>();
Published = false;
Deleted = false;
Completed = false;
}
}
我不太清楚问题所在。 难道是NHibernate的代理(我用流利NHibernate的)? 奇怪的是,如果我不使用ApiController和手动返回JSON,这工作得很好!
更新:
按下面的答案,这是我不得不在的Application_Start()添加代码:
HttpConfiguration config = GlobalConfiguration.Configuration;
((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;