could not load file or assembly System.Net.Http 4.2.0.0

0

I am really stuck on this one and could use some help.

This problem started when I added an async method involving httpclient to a class library. I call the class library from a short test console application. When it gets to the async method I get an exception:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

Originally it said Could not load System.Net.Http Version 4.1.1.2 (I had version 4.0.0.0 in the project) and I was using Framework 4.5.2. I could not find a system.net.http version 4.1.1.2 for NET Framework 4.5.2 so I first updated the class library and test projects to NFW 4.7.2 and then added a reference to the version of System.Net.Http associated with this framework (i.e. choosing it from the assemblies tab).

When I look at the properties of the dll in the class lib project (toolslib) it shows 4.2.0.0. I added the same reference to the test project used to call it and it also now shows 4.2.0.0.

Additionally I added a redirect section to the app.config for toolslib

<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 put the same lines in the app.config for the test console app.

However I still get the same issue now with it saying it can't load 4.2.0.0. I set up Fusion Logs and got the following after running and getting the exception:

*** Assembly Binder Log Entry  (7/9/2020 @ 9:12:52 PM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Users\smjhu\Documents\DevProjects\IDXUtil\Test\bin\Debug\Test.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http, Version=4.2.0.0, Culture=neutral,  ]PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Test.exe
Calling assembly : ToolsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\smjhu\Documents\DevProjects\IDXUtil\Test\bin\Debug\Test.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: Redirect found in application configuration file: 4.2.0.0 redirected to 4.2.0.0.
LOG: Post-policy reference: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http/System.Net.Http.EXE.
LOG: All probing URLs attempted and failed.

The only thing I can see from the logs is it appears to be attempting to look for the dll in the test/bin/debug folder.

I tried setting the dll to copy local but same result.

I then tried copying the system.net.http dll into the bin/debug folder manually but then when I hit that method I get a badimage exception saying I cannot use reference assemblies.

BTW also tried updating system.net.http with nuget with same result

c#
asked on Stack Overflow Jul 10, 2020 by smjhunt

1 Answer

0

If I remove the bindingredirect from the console app, the problem goes away.

Unfortunately, this does not really provide an explanation for the problem but it does at least for now resolve it.

answered on Stack Overflow Jul 10, 2020 by smjhunt

User contributions licensed under CC BY-SA 3.0