0

这是一个经典的“适用于开发但不适用于 prod”的场景,但我需要确定它为什么不能在 prod 上运行以修复它。我有一个相对简单的 Web API 调用,它返回一个对象图

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

我的课程

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; }
}

这些类都酌情用 装饰(为了节省空间DataContractDataMember我把它删掉了)。该服务在本地工作,并在其他环境中工作,包括另一个应该与问题服务器相同的 prod 环境。

我得到以下堆栈跟踪:

{"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)"}}}

奇怪的是,其他一些 Web API 调用可以在这个服务器上工作——但是它们要简单得多,例如可以检索字符串列表。

有人遇到过这个或有什么建议吗?

4

0 回答 0