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>
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With