Newtonsoft.Json装配冲突

Hir*_*ren 32 c# .net-assembly paypal-sandbox

我在我的项目中使用Netonsoft.Json.它工作正常,直到我开始在我的项目中集成Paypal SDK.我的代码如下.

         String AccessToken =
  new PayPal.OAuthTokenCredential("", "").GetAccessToken(); ---->>>> This Line Throwing An Error
            PayPal.Api.Payments.Address add = new PayPal.Api.Payments.Address();
            add.city = TextBoxCity.Text;
            add.line1 = TextBoxAddress.Text;
            add.phone = TextBoxPhoneNumber.Text;
            add.postal_code = TextBoxZipcode.Text;
            add.state = TextBoxState.Text;
            PayPal.Api.Payments.CreditCard cc = new PayPal.Api.Payments.CreditCard();
            cc.number = TextBoxCreditCardNumber.Text;
            cc.first_name = TextBoxFirstName.Text;
            cc.last_name = TextBoxLastName.Text;
            cc.expire_month = Convert.ToInt16(TextBoxExpiryMonth.Text);
            cc.expire_year = Convert.ToInt16(TextBoxExpiryYear.Text);
            cc.cvv2 = TextBoxCVVNumber.Text;
            cc.billing_address = add;
            cc.Create(AccessToken);
Run Code Online (Sandbox Code Playgroud)

我得到如下错误

       System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Run Code Online (Sandbox Code Playgroud)

我在互联网上搜索并找到了一些改变配置文件的解决方案.所以我改变我的配置文件如下

        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
     <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="4.5.0.0" />
  </dependentAssembly>
</assemblyBinding>
Run Code Online (Sandbox Code Playgroud)

我也玩堆栈属性,如复制本地,特定版本,但没有任何帮助我解决这个问题.我怎样才能解决装配冲突?

Har*_*old 62

我刚遇到同样的问题,我通过使用更新Newtonsoft.Json到最新版本来解决它

Update-Package Newtonsoft.Json
Run Code Online (Sandbox Code Playgroud)

然后转到Web.config并添加:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.8"/>
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)


Lop*_*ded 33

他的解决方案+1到zbarrier.这就是它起作用的原因......

+1给zbarrier他的答案帮助我解决了我的问题.大会参考问题是最糟糕的...所以我想我会发布我采取的步骤,以及我学到的一些东西,并希望它有所帮助:


  1. FAILED ATTEMPT:将以下几行粘贴到我的web.config:

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
            <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.8"/>
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    
    Run Code Online (Sandbox Code Playgroud)

    ^^^^^没有工作


  1. 解决方案:导航到~/Bin/Newtonsoft.Json.dllVisual Studio中并打开文件.默认情况下,该文件的界面显示一个以程序集命名的文件夹 - 我双击它以展开,最终看到:程序集文件界面然后,我双击1 [Neutral]图标,它带我到汇编的信息,在这里看到:汇编文件信息

    所说的行Assembly Version是您需要输入标签newVersion属性的内容<bindingRedirect>.所以我选择了我粘贴的部分(在第一步中)并将"5.0.8"更改为"6.0.0.0".我的新<runtime>部分看起来像这样:

      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
            <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="6.0.0.0"/>
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    
    Run Code Online (Sandbox Code Playgroud)

    它工作了^^^^^ !! 最后...


其他注释,以防任何人仍然困惑:

  • 标记位于web.config中的<runtime>标记内<configuration></configuration>.我上面显示的部分直接粘贴在我的web.config <configuration>部分的开始标记下面.
  • xmlns属性表示相应的XML命名空间.程序集的开发人员使用它来避免冲突标记的问题.在这种情况下,您应该使用xmlns="schemas-microsoft-com:asm.v1"上面列出的安全 .
  • 您可以更改该oldVersion属性以转发其他版本的程序集.例如,我可能会编辑我的看起来更像zbarrier的答案.
  • publicKeyToken是另一个属性,当涉及到Newtonsoft.Json时几乎保持不变.publicKeyToken只是公钥的缩短版本 - 就像书的标题一样 - 在这种情况下并没有真正改变.如果您想知道程序集的公钥,只需打开Developer Command Prompt可在开始菜单中找到的公钥,然后使用命令提示符导航到程序集文件的位置(在本例中~\Bin\),然后运行 sn - T assembly_file_name命令.所以在这种情况下,命令是sn -T Newtonsoft.Json.dll.您应该得到这样的回复:sn命令响应如您所见,Newtonsoft公钥(30ad4fe6b2a6aeed)就位于最后.


小智 23

我遇到了程序集版本6.0.1的同样问题.我按照Harold的指示将以下行粘贴到web.config中:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.8"/>
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

然后我删除了对Newtonsoft.Json的项目引用,并删除了packages.config文件中对Newtonsoft.Json的引用.

我打开了Nuget Manager并重新安装了Newtonsoft.Json.

安装将web.config设置更改为以下内容,一切正常:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)