Assembly mismatch despite redirecting the assembly and loading the correct version

My console application uses System.Net.Http.Formatting v5.1.0.0, which depends on Newtonsoft.Json v4.5.0.0. However, my application includes v6.0.0.0 Newtonsoft.Json (for other reasons).

To make System.Net.Http.Formatting use the new version of Newtonsoft.Json, I added reconfiguration of the assembly in App.config:

<?xml version="1.0" encoding="utf-8"?> <configuration> ... <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration> 

However, I get the following exception:

 A first chance exception of type 'System.IO.FileLoadException' occurred in System.Net.Http.Formatting.dll Additional information: 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 manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) 

The merge log shows that the correct assembly is loaded, but it fails due to a mismatch:

 *** Assembly Binder Log Entry (2014.08.10. @ 13:13:25) *** The operation failed. Bind result: hr = 0x80131040. No description available. Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll Running under executable D:\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe --- A detailed error log follows. === Pre-bind state information === LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed (Fully-specified) LOG: Appbase = file:///D:/ConsoleApplication1/bin/Debug/ LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = NULL LOG: AppName = ConsoleApplication1.exe Calling assembly : System.Net.Http.Formatting, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. === LOG: This bind starts in default load context. LOG: Using application configuration file: D:\ConsoleApplication1\bin\Debug\ConsoleApplication1.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: GAC Lookup was unsuccessful. LOG: Attempting download of new URL file:///D:/ConsoleApplication1/bin/Debug/Newtonsoft.Json.DLL. LOG: Assembly download was successful. Attempting setup of file: D:\ConsoleApplication1\bin\Debug\Newtonsoft.Json.dll LOG: Entering run-from-source setup phase. LOG: Assembly Name is: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: The assembly reference did not match the assembly definition found. ERR: Run-from-source setup phase failed with hr = 0x80131040. ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated. 

What can I do to resolve this inconsistency? Thanks in advance.

Decision

There was nothing wrong with the redirect. The only hint was that it wasn’t even used in some way, although everything seemed to work as expected (note that the log even shows that the correct configuration file was loaded). The problem was that the assemblyBinding section contained this declaration for another assembly:

 <qualifyAssembly partialName="log4net" fullName="log4net, 1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" /> 

This line solved this problem, but somehow violated Json forwarding. I don’t know why: the qualifyAssembly declaration is also considered correct.

However, deleting this declaration made the assembly redirect work ...

+8
.net-assembly
source share
2 answers

There is no evidence that your <bindingRedirect> is valid. You should see:

 LOG: Redirect found in application configuration file: 4.5.0.0 redirected to 6.0.0.0. LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed 

There is no biscuit in your question, but everything you edited seems to be missing: D: \ ConsoleApplication1 \ bin \ Debug \ ConsoleApplication1.exe.Config. Odd drive letter. Beware of a project that already has an App.config project element, and you add App1.config. Something like that.

+5
source share

I just wanted to add a small amount of additional details here, since this is a particularly unpleasant problem, and garlic, silver bullets and holy water are in short supply.

We ran into this recently and found that the reason is a violation of the XML schema in app.config as a result of poor automatic merging, for example.

 <dependentAssembly> <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> <assemblyIdentity name="Microsoft.Extensions.FileProviders.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" /> </dependentAssembly> 

We did not notice the problem in the file, as it is still technically reliable, and the Visual Studio parser never complained.

However, the binding procedure to runtime, however, refused to apply any binding redirects (even those that were specified outside the rejected section). In addition, as indicated above, there are no signs in the merge logs that the runtime was unable to parse the configuration file. It sees the correct location of the dll and configuration file, but still fails with the mismatch of minor versions, as if redirection were never applied.

TL; DR . If you get this behavior, make sure your XML configuration file is 100% perfect - and don't believe that squiggly lines in VS will show every Possible problem.

+3
source share

All Articles