Solution for .NET error: The type ‘HttpResponseMessage’ exists in both ‘System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ and ‘System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’

Got this error when building a .NET framework (version 4.6.1) solution containing multiple projects:

The type 'HttpResponseMessage' exists in both 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

My solution:
Install System.Net.Http version 4.0.0 as nuget package to the “failing project”.

Added this to the “failing” project app.config file:

<assemblyBinding>
<!-- other dependentAssembly bindings here -->
<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>
</assemblyBinding>

Updated the “failing” projects .csproj file like this:
Removed this line or similar for System.Net.Http:

<!--<Reference Include="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />-->

Added “hintpath” with projects relative path to the nuget packages.
In my example its 3 level up and then down (..\..\..) you might need to adjust to your projects folderstructure.

<Reference Include="System.Net.Http, Version=4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\System.Net.Http.4.0.0\ref\dotnet\System.Net.Http.dll</HintPath>
</Reference>

Voila.

Leave a Reply

Your email address will not be published. Required fields are marked *