【问题标题】:ServiceStack: Getting FileNotFoundException when properties are null?ServiceStack:当属性为空时获取 FileNotFoundException?
【发布时间】:2018-12-10 21:16:57
【问题描述】:

我正在使用 ServiceStack 并发现了一些奇怪的东西。当 WS Dto 包含一些 null 值时,我在 System.Numerics.Vectors 上得到 FileNotFoundException。例如,我有一个名为“GetBookingSolutions”的 Dto,如下所示:

    [Route(BookingWizard.BASE_PATH + "getbookingsolution", Verbs = "POST", Summary = "Sends booking spec to request server to create/plan the booking")]
public class GetBookingSolution : IReturn<GetBookingSolutionResponse>
{
    public DateTime TimeWanted { get; set; }
    public uint AddressFrom { get; set; }
    // ...
    public Dictionary<uint, int> Equipment { get; set; }
    public Dictionary<uint, int> CoTravellers { get; set; }
}

最后两个是字典,因此是可以为空的对象。如果发送客户端发送如下所示的 JSON:

{
  "TimeWanted": "2018-12-10T09:05:00.000Z",
  "AddressFrom": 14427162,
  "CoTravellers": null,
  "Equipment": null
}

然后我得到异常,如下所示。服务实现永远不会执行,因此代码永远不会到达。相反,我在UncaughtExceptionHandlers 中得到了异常。

如果 JSON 看起来像这样:

{
  "TimeWanted": "2018-12-10T09:05:00.000Z",
  "AddressFrom": 14427162,
}

或者如果字典被填充:

{
  "TimeWanted": "2018-12-10T09:05:00.000Z",
  "AddressFrom": 14427162,
  "CoTravellers": {
    "13486513": 2,
    "13486515": 1
   },
  "Equipment": {
   "13424459": 2,
  }
}

那么错误不会出现。为什么会这样?这是一个错误还是这里有什么建议? null 值适用于这些属性(如果最终用户未设置它们)。

System.IO.FileNotFoundException: Could not load file or assembly 'System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at System.SpanHelpers.SequenceEqual(Byte& first, Byte& second, NUInt length)
   at System.MemoryExtensions.Equals(ReadOnlySpan`1 span, ReadOnlySpan`1 other, StringComparison comparisonType)
   at ServiceStack.Text.Json.JsonTypeSerializer.EatValue(ReadOnlySpan`1 value, Int32& i)
   at ServiceStack.Text.Common.DeserializeTypeRefJson.StringToType(ReadOnlySpan`1 strType, TypeConfig typeConfig, EmptyCtorDelegate ctorFn, KeyValuePair`2[] typeAccessors)
   at ServiceStack.Text.Common.DeserializeType`1.StringToTypeContext.DeserializeJson(ReadOnlySpan`1 value)
   at ServiceStack.Text.Json.JsonReader.<>c__DisplayClass3_0.<GetParseSpanFn>b__0(ReadOnlySpan`1 v)
   at ServiceStack.Text.JsonSerializer.DeserializeFromSpan(Type type, ReadOnlySpan`1 value)
   at ServiceStack.Text.DefaultMemory.Deserialize(MemoryStream ms, Boolean fromPool, Type type, DeserializeStringSpanDelegate deserializer)
   at ServiceStack.Text.DefaultMemory.<DeserializeAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.Host.RestHandler.<CreateRequestAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.Host.RestHandler.<CreateRequestAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.Host.RestHandler.<ProcessRequestAsync>d__14.MoveNext()

【问题讨论】:

    标签: servicestack


    【解决方案1】:

    这是一个运行时依赖问题,与 DTO 中的 null 无关,这正是触发代码路径需要 System.Numerics.Vectors 依赖的原因,它无法加载.在这种情况下调用System.MemoryExtensions.Equals() 扩展方法。

    似乎有一个带有此异常的few issues,看起来this GitHub Thread 有一些您可以尝试的解决方法。

    【讨论】:

    • 好吧,我将不得不更改 JSON,那个长的 GitHub 线程对我来说没有意义 =)
    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2021-09-30
    相关资源
    最近更新 更多