休息 – 无法序列化内容类型的响应正文

前端之家收集整理的这篇文章主要介绍了休息 – 无法序列化内容类型的响应正文前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个使用控制器和ApiControllers的MVC4应用程序.我修改了默认的Web API路由以包含动作名称.当我尝试获得基准列表时,我收到此错误消息:

The ‘ObjectContent`1’ type Failed to serialize the response body for
content type ‘application/json; charset=utf-8’

InnerException是这样的(我在这种情况下返回JSON,与XML相同):

  1. "InnerException": {
  2. "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,JsonContract valueContract,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer,ISerializable value,JsonISerializableContract contract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IWrappedCollection values,JsonArrayContract contract,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": {
  3. "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)"
  4. }

这是我运行的代码

  1. // GET api/benchmark/getincomplete
  2. [HttpGet]
  3. public IList<Benchmark> GetIncomplete()
  4. {
  5. var s = HibernateModule.CurrentSession;
  6. var benchList = s.QueryOver<Benchmark>()
  7. .Where(b => !b.Completed)
  8. .Where(b => !b.Deleted)
  9. .OrderBy(b => b.Id).Asc
  10. .List<Benchmark>();
  11.  
  12. return benchList;
  13. }

这是基准模型:

  1. public class Benchmark
  2. {
  3. public virtual int Id { get; set; }
  4. [required]
  5. [DataType(DataType.Date)]
  6. public virtual DateTime Date { get; set; }
  7. [required,ScriptIgnore]
  8. public virtual IList<TestResult> Results { get; set; }
  9. [required]
  10. public virtual IList<TestCase> TestCases { get; set; }
  11. [AllowHtml]
  12. public virtual string Description { get; set; }
  13. public virtual Device Device { get; set; }
  14. public virtual bool Published { get; set; }
  15. [Display(Name = "Deleted"),ScriptIgnore]
  16. public virtual bool Deleted { get; set; }
  17. public virtual bool Completed { get; set; }
  18.  
  19. public Benchmark()
  20. {
  21. Results = new List<TestResult>();
  22. TestCases = new List<TestCase>();
  23. Published = false;
  24. Deleted = false;
  25. Completed = false;
  26. }
  27. }

我不太清楚问题所在.可能是NHibernate Proxy(我使用Fluent NHibernate)吗?奇怪的是,如果我不使用ApiController,并手动返回JSON,这很好用!

更新:

根据下面的答案,这是我在Application_Start()中添加代码

  1. HttpConfiguration config = GlobalConfiguration.Configuration;
  2. ((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;

解决方法

NHibernate框架中的 IdentityEqualityCompairer似乎有[SerializableAttribute]注释.

从对这个答案的评论来看,Why won’t Web API deserialize this but JSON.Net will?看起来,JSON.NET在WebApi使用和独立使用之间配置有所不同.

Json.NET serializer by default set the IgnoreSerializableAttribute to true. In WebAPI,we set that to false.

因此,由于您无法使用[SerializableAttribute](因为它不在代码中),您可以尝试更改默认的WebApi JSON.NET设置,以忽略它使用此答案here

  1. ((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;

也可以考虑使用DTO将您的结果发送到响应中 – 这将使您完全控制通过电线发送的对象.

猜你在找的asp.Net相关文章