与Newtonsoft.Json的Twitterizer错误:"警告:比较程序集名称导致不匹配:主要版本"

时间:2016-06-14 09:03:50

标签: c# .net json twitter twitterizer

我正在构建一个使用Newtonsoft.Json进行各种操作的C#(.NET 4.5)应用程序。我试图集成Twitterizer,但看起来它正在尝试加载更旧版本的Newtonsoft.Json,导致运行时异常。

我尝试在我的App.config中添加重定向,如下所示:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/> <bindingRedirect oldVersion="2.4.2.43046" newVersion="4.5.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>

然而,不幸的是,这还没有解决问题。这是添加重定向后的例外情况:

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) File name: 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' at Twitterizer.Core.TwitterCommand'1.ExecuteCommand() at Twitterizer.Core.CommandPerformer.PerformAction[T](ICommand'1 command) at Twitterizer.TwitterTimeline.UserTimeline(OAuthTokens tokens, UserTimelineOptions options) at (redacted).Workflow.GetMyTweets(Int32 count) in (redacted)\Workflow.cs:line 810 at (redacted).Workflow.Twitter() in (redacted)\Workflow.cs:line 787 at (redacted).Workflow.Execute(Int32 browser, Log WorkflowLog) in (redacted)\Workflow.cs:line 677

=== Pre-bind state information === LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed (Fully-specified) LOG: Appbase = file:///(redacted)/bin/Release/ LOG: Initial PrivatePath = NULL Calling assembly : Twitterizer2, Version=2.4.2.43046, Culture=neutral, PublicKeyToken=69d1469eac671567. LOG: This bind starts in default load context. LOG: Using application configuration file: (redacted)\bin\Release\(redacted).vshost.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed LOG: Attempting download of new URL file:///(redacted)/bin/Release/Newtonsoft.Json.DLL. WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

请注意,由于各种原因,我无法切换到不同的Twitter库(即Linq2Twitter等不是一个选项),也无法将我的JSON处理还原到旧库Twitterizer使用的版本。

我需要的是找到一种方法来完成这项工作而不用改变我正在使用的库/版本。

有什么想法吗?谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

当您遇到此类问题时,您可以使用的唯一解决方案是在项目中导入您要使用的Newtonsoft .dll(而不是库中的那个。)

然后在项目引用中打开dll属性。 在那里,您将看到一个名为Aliases的属性。

通过将别名从global更改为my_alias,您可以通过引用别名来使用其他版本的库。

在要使用别名库的代码文件中,您必须使用extern关键字

extern alias my_alias;
// And then
using static my_alias::Newtonsoft.Json.Linq.JToken;