2016-06-14 126 views
0

我正在构建一个C#(.NET 4.5)应用程序使用Newtonsoft.Json的各种事情。我试图整合Twitterizer,但它看起来像试图加载一个旧版本的Newtonsoft.Json,导致运行时异常。Newtonsoft.Json Twitterizer错误:“WRN:比较汇编名称导致不匹配:主要版本”

我尝试添加一个重定向到我的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

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

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

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

在您要使用的别名库中的代码文件,你将不得不使用extern关键字

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

这是一个伟大的建议!不幸的是,我已经尝试过,并没有奏效。如果我为那个较旧的DLL创建了一个单独的目录,或者如果我在运行时重新命名了它并加载了程序集,那可能会更麻烦,但这比减轻和使用与库捆绑在一起的版本更麻烦。我最终只是这么做了,因为看起来我担心的兼容性问题并不像我预期的那样糟糕。谢谢! –

+0

虽然它没有解决我的具体使用案例,但我认为你的答案仍然值得选择,因为如果我有更多时间按照我想要的方式完成此操作,它可能会起作用。 –