2016-06-14 27 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

雖然它沒有解決我的具體使用案例,但我認爲你的答案仍然值得選擇,因爲如果我有更多時間按照我想要的方式完成此操作,它可能會起作用。 –

相關問題