Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding redirect for System.Net.Http doesn't work - why?

Tags:

c#

asp.net

nuget

The project I'm working on has System.Net.Http version 4.3.3 installed, according to NuGet. It had a binding redirect in the Web.config file to redirect it to version 4.2.0.0, which worked for some reason:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>

I changed it to point to 4.3.3.0 which is the version NuGet says is installed:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.3.3.0" newVersion="4.3.3.0" />
</dependentAssembly>

... but now I get this error when I try to debug the ASP.NET site:

Could not load file or assembly 'System.Net.Http' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Net.Http | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/.../Dev/Src/Web/PortalSite/
LOG: Initial PrivatePath = C:\...\Dev\Src\Web\PortalSite\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\...\Dev\Src\Web\PortalSite\web.config
LOG: Using host configuration file: C:\...\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../Dev/Src/Web/PortalSite/bin/System.Net.Http.DLL.
LOG: Using application configuration file: C:\...\Dev\Src\Web\PortalSite\web.config
LOG: Using host configuration file: C:\...\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.2.0.0 redirected to 4.3.3.0.
LOG: Post-policy reference: System.Net.Http, Version=4.3.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../Dev/Src/Web/PortalSite/bin/System.Net.Http.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

To make things even more confusing, the DLL version in the bin directory for the site seems to be different again:

PS C:\...\Dev\Src\Web\PortalSite\bin> (Get-Item ...\bin\Debug\System.Net.Http.dll).VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
4.6.25908.02 ... 4.6.25908.02     C:\...\Dev\Src\Web\PortalSite\bin\System.Net.Http.dll

What's up with this? How did the redirect work before, and why doesn't it work now?

like image 737
Jez Avatar asked Mar 05 '18 18:03

Jez


People also ask

How does binding redirect work?

1 or a later version, the app uses automatic binding redirection. This means that if two components reference different versions of the same strong-named assembly, the runtime automatically adds a binding redirection to the newer version of the assembly in the output app configuration (app. config) file.

How do I enable and disable automatic binding redirection?

Right-click the project in Solution Explorer and select Properties. On the Application page, uncheck the Auto-generate binding redirects option. If you don't see the option, you'll need to manually disable the feature in the project file.


1 Answers

It turns out that the DLL version in my bin directory was actually version 4.2.0.0, which is apparently bundled with Visual Studio 2017, and the version distributed in NuGet package version 4.3.3 is DLL version 4.1.1.2, which wasn't being used at all. Confusing! But this explains why the redirect to DLL version 4.2.0.0 worked and redirecting to 4.3.0.0 didn't.

like image 144
Jez Avatar answered Sep 22 '22 14:09

Jez