Я пытаюсь сделать вызов ajax, чтобы получить штаты из страны, но я продолжаю получать исключение invalidcastException. Я использую MVC4 с json.NET. Я могу без проблем сериализовать объекты в своих тестах, но когда я делаю вызов ajax, я продолжаю получать сообщение об ошибке.
Это тип значения:
[JsonConverter(typeof(ValueObjectConverter))]
public class Code : IValueObject
{
private readonly string _code;
private Code(string code)
{
_code = code;
}
public override string ToString()
{
return _code;
}
public static implicit operator Code(string code)
{
return new Code(code);
}
public static implicit operator String(Code code)
{
return code.ToString();
}
}
Это мой объект:
public class CountryState : Entity
{
public Code CodeWPS { get; set; }
public Code CodeIAM { get; set; }
public Name Description { get; set; }
}
Это jsonconverter:
public class ValueObjectConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IValueObject));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return (IValueObject)existingValue;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var item = (IValueObject)value;
writer.WriteValue(item.ToString());
writer.Flush();
}
}
Это метод API:
public IEnumerable<CountryState> GetStatesByCountry(string codeType, string countryCodeIam)
{
var states = _getAllCountriesQueryHandler.Handle(countryCodeIam);
return states;
}
Это ошибка, которую я получаю с firebug:
{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Error getting value from 'CodeWPS' on 'CNH.CSCN.BBS.Entities.CountryState'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.<WriteToStreamAsync>b__c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'System.String' to type 'CNH.CSCN.BBS.Entities.ValueTypes.Code'.","ExceptionType":"System.InvalidCastException","StackTrace":" at GetCodeWPS(Object )\r\n at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"}}}
ОБНОВИТЬ:
Я добавил контрактный преобразователь:
public class SpecialContractResolver : DefaultContractResolver
{
protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var pi = (PropertyInfo)member;
if (typeof(IValueObject).IsAssignableFrom(pi.PropertyType))
{
return new IValueObjectValueProvider(member, pi.PropertyType);
}
}
else if (member.MemberType == MemberTypes.Field)
{
var fi = (FieldInfo)member;
if (typeof(IValueObject).IsAssignableFrom(fi.FieldType))
{
return new IValueObjectValueProvider(member, fi.FieldType);
}
}
return base.CreateMemberValueProvider(member);
}
}
и IValueProvider:
public class IValueObjectValueProvider : IValueProvider
{
private readonly object _defaultValue;
private readonly IValueProvider _underlyingValueProvider;
public IValueObjectValueProvider(MemberInfo memberInfo, Type underlyingType)
{
_underlyingValueProvider = new DynamicValueProvider(memberInfo);
_defaultValue = underlyingType;
}
public void SetValue(object target, object value)
{
target = value;
}
public object GetValue(object target)
{
return target.ToString();
}
}
Я знаю, что я на правильном пути, я больше не получаю сообщение об ошибке, но такой результат:
Объект { CodeWPS="CNH.CSCN.BBS.Entities.CountryState", CodeIAM="CNH.CSCN.BBS.Entities.CountryState", Description="CNH.CSCN.BBS.Entities.CountryState"}
Я думаю, что мой contractresolver и ivalueprovider еще не на 100% верны...