Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Http - Could not load file or assembly, then Cannot load a reference assembly for execution

Tags:

c#

.net

When I publish a csproj I get the message:

Could not load file or assembly System.Net.Http

If I add it to the project and republish, I get the message:

[BadImageFormatException: Cannot load a reference assembly for execution.] [BadImageFormatException: Could not load file or assembly 'System.Net.Http'

So it's like catch-22

This is the system.web of my web.config

  <system.web>
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.6" />
  </system.web>
like image 374
robertocg72 Avatar asked Oct 20 '25 14:10

robertocg72


2 Answers

First things first:

Make sure all your projects are pointing at the same version of System.Net.Http, you can manually verify this by checking your bin folder after a compile. Just make sure the DLLs are the same version.

Then Make sure all your App.Config/Web.Config have a binding redirect

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

Lastly make sure you are not accidently publishing a 32-bit DLL with an 64-bit executable. This will also cause the BadImageFormatException.

Check your project settings to make sure you are targetting the right environment, then check your dependencies to make sure they are not accidently pulling/publishing the wrong DLL.

like image 150
Victor Procure Avatar answered Oct 23 '25 05:10

Victor Procure


I solved it by install .Net framework 4.7.2 from https://support.microsoft.com/en-us/help/4054531/microsoft-net-framework-4-7-2-web-installer-for-windows

like image 44
WantToDo Avatar answered Oct 23 '25 03:10

WantToDo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!