我正在使用 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/