C# Newtonsoft.Json 程序集冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17776090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Newtonsoft.Json Assembly Conflict
提问by Hiren
I use Netonsoft.Json in my project. It works fine until I start integrating Paypal SDK in my Project. My code is as below.
我在我的项目中使用 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);
and I get error as below
我得到如下错误
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)
I search on internet and found some solution to change config file. SO I change my config file as below
我在互联网上搜索并找到了一些更改配置文件的解决方案。所以我改变我的配置文件如下
<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>
I also play around with assembly properties like Copy Local, Specific Version but nothing helps me to solve this. How Can I solve assembly conflict?
我还使用了诸如复制本地、特定版本之类的程序集属性,但没有什么能帮助我解决这个问题。如何解决装配冲突?
采纳答案by Harold
I just had the same problem and I solved it by updating the Newtonsoft.Json to the latest version using
我刚刚遇到了同样的问题,我通过使用将 Newtonsoft.Json 更新到最新版本来解决它
Update-Package Newtonsoft.Json
and then going to Web.config and adding:
然后转到 Web.config 并添加:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.8"/>
</dependentAssembly>
回答by zbarrier
I ran into the same problem for assembly version 6.0.1. I pasted the following lines into the web.config as directed by Harold:
我在汇编版本 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>
I then removed the project reference to Newtonsoft.Json and deleted the reference to Newtonsoft.Json in the packages.config file.
然后我删除了对 Newtonsoft.Json 的项目引用,并删除了 packages.config 文件中对 Newtonsoft.Json 的引用。
I opened the Nuget Manager and reinstalled Newtonsoft.Json.
我打开了 Nuget Manager 并重新安装了 Newtonsoft.Json。
The install changed the web.config settings to the following and everything worked fine:
安装将 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>
回答by Lopsided
+1 to zbarrier for his solution. Here's why it worked...
+1 zbarrier 的解决方案。这就是它起作用的原因......
+1 to zbarrier for his answer which helped me solve my issue. Assembly reference issues are the worst...so I thought I would post the steps I took, as well as some things I learned, and hopefully it helps:
+1 zbarrier 的回答帮助我解决了我的问题。汇编参考问题是最糟糕的......所以我想我会发布我采取的步骤,以及我学到的一些东西,希望它有所帮助:
FAILED ATTEMPT:Pasted the following lines into my
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>
^^^^^ DID NOT WORK
失败的尝试:将以下几行粘贴到我的
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>
^^^^^ 没有用
SOLUTION:Navigated to
~/Bin/Newtonsoft.Json.dll
, and opened the file in Visual Studio. By default, the interface for the file displays a folder named after the assembly--I double clicked it to expand, and eventually saw this:Then, I double-clicked on the
1 [Neutral]
icon which brought me to the assembly's information, seen here:The line that says
Assembly Version
is what you'll need to enter into thenewVersion
attribute of the<bindingRedirect>
tag. So I took the section I pasted (in step one) and change the "5.0.8" to "6.0.0.0". My new<runtime>
section looks like this:<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 WORKED!! Finally...
解决方案:导航到
~/Bin/Newtonsoft.Json.dll
,并在 Visual 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>
^^^^^ 成功了!!最后...
Other notes in case anyone is still confused:
其他注意事项,以防有人仍然感到困惑:
- the
<runtime>
tag goes within the<configuration></configuration>
tag in the web.config. The section I show above was pasted directly below the opening tag of my web.config's<configuration>
section. - the
xmlns
attribute represents the corresponding XML namespace. This is used by the developers of the assembly to avoid issues with conflicting tags. In this case you should feel safe using thexmlns="schemas-microsoft-com:asm.v1"
listed above. - you can alter the
oldVersion
attribute to forward additional versions of the assembly. For example, I will probably edit mine to look more like zbarrier's answer. - the
publicKeyToken
is another attribute that pretty much stays the same when it comes to Newtonsoft.Json. The publicKeyToken is just a shortened version of the public key--much like a title is to a book--and in this case doesn't really change. If you ever want to know the public key to an assembly, just open theDeveloper Command Prompt
which can be found in the start menu, then use the command prompt to navigate to the location of the assembly file (in this case~\Bin\
), and run the sn -T assembly_file_namecommand. So in this case, the command was sn -T Newtonsoft.Json.dll. You should get a response like this:As you can see, the Newtonsoft public key (30ad4fe6b2a6aeed) is located right there at the end.
- 该
<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。您应该得到这样的响应:如您所见,Newtonsoft 公钥 ( 30ad4fe6b2a6aeed) 位于末尾。
回答by Gotcha
At first , i thought my case was the same old assembly reference ... did the uninstall, reinstall, force install, rebuild, add redirect assembly ...etc
起初,我以为我的案例是相同的旧程序集参考...是否卸载,重新安装,强制安装,重建,添加重定向程序集...等
Nothing worked until, I found out that another assembly was causing the problem.
没有任何效果,直到我发现另一个程序集导致了问题。
In my case, my code was failing when I called the HttpClient.PostAsJsonAsync(requestURI, T ) method. The error about Assembly Reference threw me off since my Solution has multiple projects and in which some of the projects used an old version... ended wasting a lot of time until...
就我而言,当我调用 HttpClient.PostAsJsonAsync(requestURI, T ) 方法时,我的代码失败了。关于程序集引用的错误让我失望,因为我的解决方案有多个项目,其中一些项目使用旧版本......结束了浪费大量时间,直到......
My solution:
我的解决方案:
- Removed the existing System.Net.Http.Formatting from my References
- Installed the Install-Package Microsoft.AspNet.WebApi.Client - which installed the required Http.Formatting.
- 从我的参考文献中删除了现有的 System.Net.Http.Formatting
- 安装了安装包 Microsoft.AspNet.WebApi.Client - 安装了所需的 Http.Formatting。
Once installed, the PostAsJsonAsync() worked as expected!
安装后, PostAsJsonAsync() 按预期工作!
Hope this saves someone time I've lost looking for a solution!
希望这可以节省我寻找解决方案的时间!
回答by Apex ND
I faced the same problem, I have installed Newtonsoft.Json v9.0.1 , sandcastle stops the build displaying the same error but with version difference: "Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0,"
我遇到了同样的问题,我安装了 Newtonsoft.Json v9.0.1 ,sandcastle 停止构建,显示相同的错误但版本不同:“无法加载文件或程序集'Newtonsoft.Json,版本 = 6.0.0.0,”
what worked: find/create a project with newtonsoft.json with the version SandCastle is asking for, add the file "Newtonsoft.Json.dll" as a reference to the SC project then build. (you can find the dll in the bin folder of the project)
什么工作:使用 newtonsoft.json 找到/创建一个具有 SandCastle 要求的版本的项目,添加文件“Newtonsoft.Json.dll”作为对 SC 项目的引用,然后构建。(可以在项目的bin文件夹中找到dll)