Newtonsoft.Json装配冲突

时间:2013-07-21 19:35:30

标签: 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);

我收到错误如下

       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)

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

        <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>

我还使用像Copy Local,Specific Version这样的程序集属性,但没有什么能帮助我解决这个问题。我怎样才能解决装配冲突?

5 个答案:

答案 0 :(得分:62)

我遇到了同样的问题,我通过使用

将Newtonsoft.Json更新到最新版本来解决它
Update-Package Newtonsoft.Json

然后转到Web.config并添加:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.8"/>
</dependentAssembly>

答案 1 :(得分: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>
    

    ^^^^^无法工作


    1. 解决方案: 导航到~/Bin/Newtonsoft.Json.dll,然后打开文件 视觉工作室。默认情况下,该文件的界面显示一个文件夹 以汇编命名 - 我双击它以展开,然后 最终看到了这个:assembly-file interface然后,我双击了 在1 [Neutral]图标上,我带来了汇编信息, 在这里看到:assembly-file information

      表示Assembly Version的行是您需要输入的内容 进入newVersion标记的<bindingRedirect>属性。所以我接受了 我粘贴的部分(在第一步中)并更改了&#34; 5.0.8&#34;至 &#34; 6.0.0.0&#34 ;.我的新<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>
      

      ^^^^^ IT工作!!最后...


    2. 其他注意事项以防任何人仍然困惑:

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

答案 2 :(得分: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>

然后我删除了对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>

答案 3 :(得分:2)

起初,我认为我的案例是相同的旧程序集引用...卸载,重新安装,强制安装,重建,添加重定向程序集......等等

直到没发生任何事情,我发现另一个组件导致了这个问题。

就我而言,当我调用HttpClient.PostAsJsonAsync(requestURI,T)方法时,我的代码失败了。关于Assembly Reference的错误让我失望,因为我的解决方案有多个项目,其中一些项目使用了旧版本...结束了浪费了很多时间,直到......

我的解决方案:

  • 从我的参考文献
  • 中删除了现有的System.Net.Http.Formatting
  • 安装了Install-Package Microsoft.AspNet.WebApi.Client - 其中 安装了所需的Http.Formatting。

安装后,PostAsJsonAsync()按预期工作!

希望这可以节省时间我找不到解决方案!

答案 4 :(得分:0)

我遇到了同样的问题,我安装了Newtonsoft.Json v9.0.1,sandcastle停止构建显示相同的错误但版本不同: &#34;无法加载文件或程序集&#39; Newtonsoft.Json,Version = 6.0.0.0,&#34;

工作原理:使用SandCastle要求的版本使用newtonsoft.json查找/创建项目,添加文件&#34; Newtonsoft.Json.dll&#34;作为SC项目的参考然后构建。 (你可以在项目的bin文件夹中找到dll)