我有一个POCO类,它继承了几个类,为其提供INotifyPropertyChanged和DataAnnotations支持。当WebApi返回Court的实例时,JSON.NET序列化程序在ModelPropertyAnnotationsValidation中阻塞匿名方法委托,并出现异常(显示Fiddler的RAW响应):
{"Message":"发生错误。","ExceptionMessage":"ObjectContent"1"类型未能序列化的响应正文内容类型‘application/json;charset=utf-8'。","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An出现错误。","ExceptionMessage":"从中获取值时出错'CS$<>9_上的CachedAnonymousMethodDelegate5''Sample.Data.Models.Court'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":"位于Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象目标)\r\nNewtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriterwriter,对象值,JsonContainerContract合同,JsonProperty会员,JsonProperty财产,JsonContract&memberContract,Object&memberValue)\r\nNewtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriterwriter,对象值,JsonObjectContract合同,JsonProperty成员,JsonContainerContract集合Contract,JsonPropertycontainerProperty)\r\n位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriterwriter,Object value,JsonContract valueContract,JsonProperty成员,JsonContainer合同容器合同,JsonPropertycontainerProperty)\r\n位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriterjsonWriter,对象值)\r\n位于Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriterjsonWriter,对象值)\r\n位于System.Net.Http.Formating.JsonMediaTypeFormatter。<>c_DisplayClassd.b_c()\r\n位于System.Threading.Tasks.TaskHelpers.RunSynchronously(操作操作,CancellationToken令牌)","InnerException":{"Message":"出现错误发生。","ExceptionMessage":"公共语言运行时检测到无效的程序","ExceptionType":"System.InvalidProgramException","StackTrace":"在GetCS$<>9_CachedAnonymousMethodDelegate5(对象)\r\n位于Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象目标)"}}
法庭类别(为简洁起见进行了编辑):
using System;
using System.Collections.Generic;
using Sample.Data.Models.Infrastructure;
namespace Sample.Data.Models
{
[Serializable]
public partial class Court : ModelPropertyAnnotationsValidation
{
public Court()
{
}
private int _courtID;
public int CourtID
{
get { return _courtID; }
set
{
_courtID = value;
OnPropertyChanged(() => CourtID);
}
}
}
}
这是问题所在的继承抽象类(如果我更改public string this[string columnName]
上的getter以返回一个空字符串,它就会起作用:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Sample.Data.Models.Infrastructure
{
public abstract class ModelPropertyAnnotationsValidation : ModelBase
{
public string this[string columnName]
{
get
{
var type = GetType();
var modelProperties = TypeDescriptor.GetProperties(type).Cast<PropertyDescriptor>();
var enumerable = from modelProperty in modelProperties.Where(modelProp => modelProp.Name == columnName)
from attribute in modelProperty.Attributes.OfType<ValidationAttribute>().Where(attribute => !attribute.IsValid(modelProperty.GetValue(this)))
select attribute.ErrorMessage;
return enumerable.FirstOrDefault();
}
private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
}
public string Error
{
get { return null; }
private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
}
public virtual bool IsValid
{
get
{
var validationContext = new ValidationContext(this, null, null);
var valid = Validator.TryValidateObject(this, validationContext, null, validateAllProperties: true);
return valid;
}
private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
}
}
}
为了完整起见,这里是ModelBase:
using System;
using System.ComponentModel;
using System.Linq.Expressions;
namespace Sample.Data.Models.Infrastructure
{
public abstract class ModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(Expression<Func<object>> property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(BindingHelper.Name(property)));
}
}
}
}
WebApi控制器(为此目的非常简单):
using System.Web.Http;
using Sample.Data.Models;
namespace Sample.Services.Api.Controllers
{
public class CourtsController : ApiController
{
// GET api/values
public Court Get()
{
return new Court {Abbreviation = "TEST", FullName = "Test Court", Active = true};
}
}
}
我如何才能获得我假设的匿名委托通过序列化。。。是否忽略?
您正在使用哪个版本的Json.NET?如果您没有使用最新版本(5.0.8),请升级到它并再次运行您的应用程序。