Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

"Could not load System.Net.Http, Version=4.2.0.0" on .NET 4.7.1 together with "System.Collections.Immutable" #891

Closed
GSPP opened this issue Oct 5, 2018 · 6 comments

Comments

@GSPP
Copy link

GSPP commented Oct 5, 2018

I will attach a repro solution that I have created as follows:

  1. New web application targeting 4.7.1 on WIndows 7 with .NET 4.7.1
  2. Add a class library
  3. Reference the library from the web app and call code in it
  4. Add a few packages to both projects
  5. Add the System.Net.Http package to both projects and use it (see Class1)

When this application is started it immediately crashes:

Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Findings/bugs:

  1. System.Net.Http was not copied to the bin folder of the web app which is wrong.
  2. All projects are configured to reference version 4.1.1.2 from /packages. Yet the compiled class library DLL references 4.2.0.0. This appears wrong to me.

If you now uninstall the seemingly unrelated (and unused) package System.Collections.Immutable the project starts working!

Currently, on the latest Visual Studio and on .NET 4.7.1 it seems very easy to run into almost unfixable tooling bugs. It really is not very hard to trigger the conditions for these issues. I suspect many people are running into this and indeed, when you search the web or this issue tracker there are many reports of very strange issues around .NET 4.7.1 and System.Net.Http, System.Runtime and a few others.

These bugs are extremely hard to track down with no clear workaround. It took me two days now to investigate multiple such issues. Someone new to .NET couldn't fix these in 10 years.

NuGetHttpRepro.zip

@GSPP
Copy link
Author

GSPP commented Oct 5, 2018

I'm tracking related issues and information in a list. They are related to System.Net.Http, System.Runtime, System.IO, System.ValueTuple, System.Buffers and others.

https://github.com/dotnet/corefx/issues/32587
https://github.com/dotnet/corefx/issues/32561
#481
#567
#558
#887
#891
https://github.com/dotnet/corefx/issues/32610
https://github.com/dotnet/corefx/issues/30642
https://github.com/dotnet/corefx/issues/32757
#895
#877
#521
#295
#476
#184
#936
#941
https://github.com/dotnet/corefx/issues/33148
NuGet/Home#7440
https://github.com/dotnet/corefx/issues/31532
https://github.com/dotnet/corefx/issues/22781
https://github.com/dotnet/corefx/issues/23306
https://github.com/dotnet/corefx/issues/29622
https://github.com/dotnet/corefx/issues/9846
https://github.com/dotnet/corefx/issues/17522
https://github.com/dotnet/corefx/issues/25773

All of these have the same very few underlying issues.

.NET 4.7.2 helps with some but not all of these. You can look at my comments on some of these issues for some ideas on how to work around those problems. I also have a central list of ideas to try.

@joperezr
Copy link
Member

joperezr commented Oct 5, 2018

I just tried your repro solution, and launching it works fine for me, and I made sure that the routeconfig method was being called so that Class1 was returning in fact an HTTPClient back. Two things I changed in order to make it work:

  • I changed both of your projects from packages.config, to instead use PackageReference. This will ensure that the dependency flow between both of your projects work correctly, and that we user the nuget versions that you actually require across all your different projects, as opposed to not seeing the transitive dependencies from your web app. I'm not sure if this change is required, but its just good practice to move into the new PackageReference model since using packages.config is very prone to all sorts of errors. I'll include in this issue the ItemGroup sections of each project that contains the conversion so that you can try it yourself.
  • The other thing is that I added the suggested binding redirects from VS to your web.config. We do this automatically for other project types, but for web projects we can't really hook up with your config file and add the binding redirects, so what we do is that we throw a warning when you build in VS (which you might have seen already) and just double clicked it in order to get the redirects added to your web.config file.

After both of those minimal changes, the app ran just fine.

@joperezr
Copy link
Member

joperezr commented Oct 5, 2018

ItemGroup section for the NuGetHttpRepro.csproj:

  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.Composition" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Web.DynamicData" />
    <Reference Include="System.Web.Entity" />
    <Reference Include="System.Web.ApplicationServices" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Web" />
    <Reference Include="System.Web.Extensions" />
    <Reference Include="System.Web.Abstractions" />
    <Reference Include="System.Web.Routing" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Web.Services" />
    <Reference Include="System.EnterpriseServices" />
    <Reference Include="System.Net.Http.WebRequest" />
    <PackageReference Include="Antlr" Version="3.5.0.2" />
    <PackageReference Include="AWSSDK.Core" Version="3.3.26" />
    <PackageReference Include="bootstrap" Version="4.1.3" />
    <PackageReference Include="jQuery" Version="3.3.1" />
    <PackageReference Include="jQuery.Validation" Version="1.17.0" />
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.7.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.Agent.Intercept" Version="2.4.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.7.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.7.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.Web" Version="2.7.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.7.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.7.2" />
    <PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.6" />
    <PackageReference Include="Microsoft.AspNet.Razor" Version="3.2.6" />
    <PackageReference Include="Microsoft.AspNet.TelemetryCorrelation" Version="1.0.4" />
    <PackageReference Include="Microsoft.AspNet.Web.Optimization" Version="1.1.3" />
    <PackageReference Include="Microsoft.AspNet.WebPages" Version="3.2.6" />
    <PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" Version="2.0.1" />
    <PackageReference Include="Microsoft.jQuery.Unobtrusive.Validation" Version="3.2.10" />
    <PackageReference Include="Microsoft.Web.Infrastructure" Version="1.0.0.0" />
    <PackageReference Include="Modernizr" Version="2.8.3" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
    <PackageReference Include="popper.js" Version="1.14.3" />
    <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.1" />
    <PackageReference Include="System.IO" Version="4.3.0" />
    <PackageReference Include="System.Net.Http" Version="4.3.3" />
    <PackageReference Include="System.Runtime" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
    <PackageReference Include="System.Security.Cryptography.Encoding" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.Primitives" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2" />
    <PackageReference Include="WebGrease" Version="1.6.0" />
  </ItemGroup>

And for NuGetHttpRepro.Library

  <ItemGroup>
    <Reference Include="PresentationCore" />
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.Composition" />
    <Reference Include="System.configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Net" />
    <Reference Include="System.Numerics" />
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Security" />
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.Web" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
    <PackageReference Include="System.IO" Version="4.3.0" />
    <PackageReference Include="System.IO.Compression" Version="4.3.0" />
    <PackageReference Include="System.Net.Http" Version="4.3.3" />
    <PackageReference Include="System.Runtime" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
    <PackageReference Include="System.Security.Cryptography.Encoding" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.Primitives" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2" />
  </ItemGroup>

@joperezr
Copy link
Member

joperezr commented Oct 5, 2018

Don't forget to run msbuild /t:Restore NuGetHttpRepro.sln after changing the ItemGroup sections to restore the packages

@GSPP
Copy link
Author

GSPP commented Oct 8, 2018

Thank you for the time you have taken on this issue!

I find that my repro stops working (in a VM) under Windows 10 .NET 4.7.2. I am currently unable to migrate to 4.7.2 on my main machine, though. So feel free to close this issue since there's nothing to fix.

Migrating to PackageReference is certainly an interesting option. On my real project there are a few things blocking me from doing that:

  1. The migration feature is not available for web applications (XXX).
  2. I'm getting an error where two assemblies define the same type name (Zlib.Portable and DotNetZip do that). So far I was unable to track down why that error appeared.
  3. I'm using projects with an install.ps1 (Microsoft.SqlServer.Types).

I have tried to migrate NuGetHttpRepro to PackageReference according to your instructions. I was able to migrate the library project but the web project did not quite work. I deleted all bin, obj and .vs folders from the NuGetHttpRepro solution after using the VS feature to migrate the library to PackageReference and after using your XML to alter my csproj file. VS now does not show any package references and the build fails because System.Net.Http is not found. I wonder what I did wrong to cause this. Anyway, I'm giving up on this for now.

If you want feedback on the migration feature: Maybe users should be able to keep individual packages in the packages.config format. I'd be able to solve the Microsoft.SqlServer.Types that way. Also, a checkbox to turn off the backup would be a good idea. Serious development always happens under source control.

since using packages.config is very prone to all sorts of errors

That appears entirely correct and I will aim at migrating to PackageReference once that becomes feasible. But currently PackageReference introduces new problems as well, so that path is barred for the time being (or would come with significant effort).

For now I'm happy that I have reached a state on my real project that works at all. It involved manually changing references and binding redirects contrary to what the tooling did. I'm sure some of the issues that I experienced were user errors. I'm also sure there are tooling bugs. Weird things happen from time to time. As a customer I feel this technology is very promising but also not very mature yet.

Feel free to close this issue.

@wtgodbe
Copy link
Member

wtgodbe commented May 30, 2019

Closing as dupe of #936

@wtgodbe wtgodbe closed this as completed May 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants