Web API Serialization failure on a specific server

0

This is a classic "works on dev but not on prod" scenario but I need to identify why it isn't working on prod in order to fix it. I have a relatively simple Web API call which returns an object graph

public class ValuesController : ApiController
{
    public TimeSeriesData GetValues(string id) { /* get from database */ }
}

My classes

public class TimeSeriesData
{
    public string Name { get; set; }
    public List<Foo> KPIs { get; set; }
}

public class Foo
{
    public string Name { get; set; }
    public List<Bar> Values { get; set; }
}

public class Bar
{
    public double Actual { get; set; }
    public double Target { get; set; }
}

These classes are all decorated with DataContract, DataMember as appropriate (I've cut this down to save space). The service works locally, and works on other environments including another prod environment that should be the same as the problem server.

I get the following stack trace:

{"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 while copying content to a stream.",
    "ExceptionType":"System.Net.Http.HttpRequestException",
    "StackTrace":null,
    "InnerException":
        {"Message":"An error has occurred.",
        "ExceptionMessage":"Could not load file or assembly 'System.Runtime.Serialization, Version=2.0.5.0, 
            Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. 
            The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)",
        "ExceptionType":"System.IO.FileLoadException",
        "StackTrace":"   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
            at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
            at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
            at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
            at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
            at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
            at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
            at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociateMetadataTypeFromAttribute(Type type)
            at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
            at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
            at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[T](Type type)
            at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
            at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
            at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
            at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
            at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteStartArray(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
            at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
            at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, 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)"}}}

Strangely enough, some other Web API calls work on this server - however they are much simpler, for example a list of strings is able to be retrieved.

Anyone encountered this or have any suggestions?

serialization
.net-4.0
iis-7.5
asp.net-web-api
asked on Stack Overflow Oct 18, 2012 by Kirk Broadhurst

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0