【问题标题】:Twitterizer Error with Newtonsoft.Json: "WRN: Comparing the assembly name resulted in the mismatch: Major Version"Newtonsoft.Json 的 Twitterizer 错误:“警告:比较程序集名称导致不匹配:主要版本”
【发布时间】:2016-10-14 22:04:38
【问题描述】:

我正在构建一个 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。

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

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

【问题讨论】:

    标签: c# .net json twitter twitterizer


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 这是一个很好的建议!不幸的是,我已经尝试过了,但没有成功。如果我仅为那个较旧的 DLL 创建了一个单独的目录,或者如果我重命名它并在运行时加载程序集,则可能会这样,但这比仅仅缓和并使用与库捆绑的版本更麻烦。我最终只是这样做了,因为看起来我担心的兼容性问题并没有我预期的那么糟糕。谢谢!
    • 虽然它没有解决我的具体用例,但我认为您的答案仍然值得选择,因为如果我有更多时间完全按照我想要的方式进行操作,它可能会起作用.
    猜你喜欢
    • 2020-01-25
    • 2013-06-18
    • 1970-01-01
    • 2020-07-11
    • 2012-09-04
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多