Newtonsoft.Json 的 Twitterizer 错误:"WRN: Comparing the assembly name resulted in the mismatch: Major Version"

Twitterizer Error with Newtonsoft.Json: "WRN: Comparing the assembly name resulted in the mismatch: Major Version"

我正在构建一个 C# (.NET 4.5) 应用程序,它使用 Newtonsoft.Json 来处理各种事情。我正在尝试集成 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 使用的旧库版本。

我需要的是在不改变我正在使用的 libraries/versions 的情况下找到一种方法来完成这项工作。

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

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

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

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

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

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