0

I'm getting the following error only on live server, while calling function using WebApi, I'm using 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)"
        }
    }
}
Ilija Dimov
  • 5,221
  • 7
  • 35
  • 42
Ayman Barhoum
  • 1,255
  • 1
  • 16
  • 31
  • There must be something related to data on your live db.. Take backup of live and try to reproduce on local by debugging it. – Harsh Baid May 30 '14 at 18:46
  • I already pointed connection string to live database, and still getting the same error. – Ayman Barhoum May 30 '14 at 19:07
  • I guess any character in the content is causing this issue.. Check by debugging and then try to serialize that certain chunk of data in isolated application. – Harsh Baid May 31 '14 at 04:45
  • Also chk more links here http://stackoverflow.com/a/18134380/468718 and http://stackoverflow.com/a/14056232/468718 – Harsh Baid May 31 '14 at 04:48

1 Answers1

3

Your BaseItem is causing you some issues, if you dont need any property with BaseItem type, do the following:

1- Create new class for 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- add this code on your routing configuration setup.

var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new JsonContractResolver();
Ahmed Okour
  • 2,392
  • 1
  • 16
  • 31