将Iso8601TimeSpanConverter添加到JsonConverter列表将引发ArrayTypeMismatch异常 [英] Adding Iso8601TimeSpanConverter to JsonConverter list throws ArrayTypeMismatch exception

查看:85
本文介绍了将Iso8601TimeSpanConverter添加到JsonConverter列表将引发ArrayTypeMismatch异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using AutoRest to auto generate a c# class for a REST API from a Swagger definition file.

问题在于,当执行客户端类的初始化方法时,它将在以下代码中引发ArrayTypeMismatch异常:

The problem is that when the client class initialize methos is executed, it throws an ArrayTypeMismatch exception in the following code:

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()
        }
}

有问题的代码是添加到JsonConverters列表中的Iso8601TimeSpanConverter.

The offending piece of code is the Iso8601TimeSpanConverter added to the list of JsonConverters.

顺便说一句,这仅在VSIX软件包中运行此代码时发生.在独立应用程序上,它可以正常工作.

BTW, this only happens when running this code in a VSIX package. On a standalone app, it works fine.

我想知道这是否与Newtonsoft.Json加载和碰撞的多个版本有关吗?

I wonder if it has to do with multiple versions of Newtonsoft.Json loading and colliding?

有什么想法吗?

推荐答案

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

I encounter the same problem but finally get it resolve by using app.config to unify the Newtonsoft.Json binding.

<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上使用的版本不同.

The issue is that Iso8601TimeSpanConverter might be inheriting a different version of JsonConverter than the one you are using on the AutoRest.

这篇关于将Iso8601TimeSpanConverter添加到JsonConverter列表将引发ArrayTypeMismatch异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆