c# - 将 Iso8601TimeSpanConverter 添加到 JsonConverter 列表会抛出 ArrayTypeMismatch 异常

标签 c# json.net swagger vsix autorest

我正在使用 AutoRest 从 Swagger 定义文件为 REST API 自动生成 C# 类。

问题是在执行客户端类initialize方法时,在如下代码中抛出ArrayTypeMismatch异常:

SerializationSettings = new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    DateFormatHandling = DateFormatHandling.IsoDateFormat,
    DateTimeZoneHandling = DateTimeZoneHandling.Utc,
    NullValueHandling = NullValueHandling.Ignore,
    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
    ContractResolver = new ReadOnlyJsonContractResolver(),
    Converters = new List<JsonConverter>
        {
            new Iso8601TimeSpanConverter()
        }
}

有问题的代码是添加到 JsonConverter 列表中的 Iso8601TimeSpanConverter。

顺便说一句,只有在 VSIX 包中运行此代码时才会发生这种情况。在独立应用程序上,它运行良好。

请问是不是和多个版本的Newtonsoft.Json加载碰撞有关?

有什么想法吗?

最佳答案

我遇到了同样的问题,但最终通过使用 app.config 统一 Newtonsoft.Json 绑定(bind)解决了这个问题。

<configuration>
  <runtime>
    <assemblyBinding>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

问题是 Iso8601TimeSpanConverter 继承的 JsonConverter 版本可能与您在 AutoRest 上使用的版本不同。

关于c# - 将 Iso8601TimeSpanConverter 添加到 JsonConverter 列表会抛出 ArrayTypeMismatch 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39339605/

相关文章:

c# - 将 C++ map 数据传递给 C#

c# - 自动属性初始化器不起作用?

css - 注入(inject) SwaggerUi 自定义 CSS 但在浏览器中无效

spring-mvc - 将 Swagger 服务器 stub 添加到现有 Spring 应用程序中

c# - 单线程且无递归的 C# 中的奇怪死锁

c# - 如何以编程方式创建 NDepend 项目文件?

c# - 通过 GRAPH 更新 Azure AD 中的用户时出现 Json 序列化错误

json - 将多个值反序列化为单个属性

c# - C# .net 中的 Json 对象

testing - 不允许PUT-带有Swagger的REST保证