Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Http type mismatch at compile

Since updating a .Net Framework Web Api2 project to .Net Framework 4.7.1 and updating references to the now native System.Net.Http library, we now get the following error at compile time (not the typical runtime). Thus assembly binding redirects have no effect.

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' SkinOnline.Web C:_dev\MapsOnline.Web\API\Controllers\Utilites\LongProcessController.cs

I've no idea where the older version is coming from - perhaps one of the project dependencies.

Edit So after adding the dll from

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.1

Once under references the path has changed to

C:\VS2017\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net471\lib\System.Net.Http.dll

And that says the Version is 4.2.0.0

like image 461
jenson-button-event Avatar asked Jan 11 '18 10:01

jenson-button-event


2 Answers

So in fact the new native file is version 4.2.0.0 (no idea what the folks were doing here given the latest version before this consolidation was 4.3.3).

Indeed we do require the binding redirect.

<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>

Without it I get weird runtime errors

System.MissingMethodException Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'.

like image 101
jenson-button-event Avatar answered Nov 15 '22 21:11

jenson-button-event


The problem you are hitting is that even though the new System.Net.Http binary (4.2.0.0) is copied in the same folder of the app, the right binding redirects are not being generated automatically by the VS tooling. We recently fixed this with this pull request: https://github.com/dotnet/corefx/pull/25786

Once that fix is consumed by Visual Studio, the right binding redirect will be generated automatically for you and you won't see this issue anymore. This fix will be included in Visual Studio 15.6 Preview 3. For the time being, adding the binding redirect manually will do the trick.

like image 31
Jose Perez Rodriguez Avatar answered Nov 15 '22 21:11

Jose Perez Rodriguez