I'm building a C# (.NET 4.5) application that uses Newtonsoft.Json for various things. I'm trying to integrate Twitterizer, but it looks like it's trying to load a much older version of Newtonsoft.Json, causing a runtime exception.
I tried adding a redirect to my App.config, as follows:
<?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>
Unfortunately, however, that has not solved the issue. Here's the exception after the redirect was added:
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.
Please note that, for a variety of reasons, I can't switch to a different Twitter library (i.e. Linq2Twitter et al are not an option), nor can I revert my JSON handling to the older library version used by Twitterizer.
What I need is to find a way to make this work without changing what libraries/versions I'm using.
Any ideas? Thanks for your help!
When you encounter such an issue, the only solution you can use is to import in your project the Newtonsoft .dll that you want to use (not the one for the library).
Then open the dll properties in your project references. There you will see a property called Aliases.
By changing the alias from global
to my_alias
, you will be able to use the other version of the library by referencing your alias.
In the code files where you want to use the aliased library you will have to use the extern
keyword
extern alias my_alias;
// And then
using static my_alias::Newtonsoft.Json.Linq.JToken;