Could not load file or assembly Newtonsoft.Json 4.5.0.0

Hi,

I am unable to use hangfire in ASP.NET web form application after upgrading newtonsoftjson to 6.0.8. It was working with 4.5.0.0 version before. Now due to azure storage nuget dependency I need 6.0.8 version in newtonsoftjson version and it gives out the following error:

Assembly load trace

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = <...>/src/Web/
LOG: Initial PrivatePath = <...>\src\Web\bin
Calling assembly : Hangfire.Core, Version=1.7.6.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: <...>\src\Web\web.config
LOG: Using host configuration file: <...>\IISExpress\config\aspnet.config
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:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/vs/c9f16d42/c2873b0b/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/vs/c9f16d42/c2873b0b/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL <...>/src/Web/bin/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.

Stack trace:

[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)]
Hangfire.Dashboard.Pages.HomePage.Execute() +0
Hangfire.Dashboard.RazorPage.TransformText(String body) +26
Hangfire.Dashboard.RazorPage.ToString() +7
Hangfire.Dashboard.RazorPageDispatcher.Dispatch(DashboardContext context) +65
Hangfire.Dashboard.<>c__DisplayClass1_1.b__1(IDictionary`2 env) +430
Microsoft.Owin.Mapping.d__0.MoveNext() +274
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +182
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2.MoveNext() +180
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +380
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159

Note: Binding redirect is added in web.config and previous version NewtonsoftJson dll is also removed.

You’ll need to update your we.config to use a binding redirect. If that doesn’t work, there are more detailed instructions here

I wrote a program to fix this issue for you automatically: https://github.com/BackTrak/DependencyFixup/releases/tag/1.0.0.0