0

我只在实时服务器上收到以下错误,在使用 WebApi 调用函数时,我使用的是 Sitecore glass mapper。

{
    "$id": "1",
    "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": {
        "$id": "2",
        "Message": "An error has occurred.",
        "ExceptionMessage": "Error getting value from 'proxyGenerationOptions' on 'Castle.Proxies.BaseItemProxy'.",
        "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, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\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, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)",
        "InnerException": {
            "$id": "3",
            "Message": "An error has occurred.",
            "ExceptionMessage": "Common Language Runtime detected an invalid program.",
            "ExceptionType": "System.InvalidProgramException",
            "StackTrace": " at GetproxyGenerationOptions(Object )\r\n at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
        }
    }
}
4

1 回答 1

3

您的 BaseItem 给您带来了一些问题,如果您不需要 BaseItem 类型的任何属性,请执行以下操作:

1- 为 Json Contract Resolver 创建新类。

public class JsonContractResolver : DefaultContractResolver
{
public JsonContractResolver():base()
{

}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
    IList<JsonProperty> filtered = new List<JsonProperty>();

    foreach (JsonProperty p in base.CreateProperties(type, memberSerialization))
     {
        if (p.PropertyType != typeof(BaseItem))
          filtered.Add(p);

         return filtered;
     } 
}
}

2-在您的路由配置设置中添加此代码。

var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new JsonContractResolver();
于 2014-06-01T11:53:18.660 回答