Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The referenced component 'System.Net.Http' could not be found

I've created a new project in VS2017 targeting the .NET Framework 4.6.1, and when I add a NuGet package reference to System.Net.Http, I get the following warning for it and all of its depencencies:

The referenced component 'System.Net.Http' could not be found.

The project (and its solution) compile but when I run, I get an exception because the assembly can't be found when trying to instantiate an instance of HttpClient.

I have confirmed that the HintPath is pointing to the correct location in the packages folder and the DLL is there with a size of 193 KB. I've removed the reference, and tried to add the reference to the DLL directly without success.

I've also tried to add a reference to through Framework > Assemblies, but no matter what I do, the reference has a warning icon, and the properties for the reference look like this:

System.Net.Http Reference Properties

I've tried the following:

  • Remove and add the NuGet reference back
  • Remove the reference, clear the NuGet cache, and add the NuGet reference back
  • Used Update-Package -reinstall with the appropriate arguments
  • Copy the solution to another computer and try there (with the same result)

It seems that no matter what I try, I can't get the reference to work. What other things should I be trying?

like image 204
Adrian Anttila Avatar asked Feb 05 '23 04:02

Adrian Anttila


2 Answers

TLDR - Service Fabric services don't work well with project references to .NET Standard packages; instead, use the full .NET Framework for class libraries.


After several more hours of trial and error, I was able to determine that the issue was not with the System.Net.Http package itself, but was the result of a reference to another project within the solution.

If I referenced the project targeting netstandard1_6, the System.Net.Http references (including the dependent packages) would not load or resolve. Once I removed the project reference, they loaded fine! Ultimately, I changed my referenced project to a "regular" .NET Framework project, added a reference to that version, and all my references were fine.

There are a few things that we suspect, but aren't sure which was the ultimate cause:

  • The project file format for .NET Standard is different than .NET Framework
  • Service Fabric projects require x64, and .NET Standard projects target Any CPU

Any additional explanation would be appreciated!

like image 110
Adrian Anttila Avatar answered Feb 06 '23 17:02

Adrian Anttila


Today I ran into this issue. In my case the cause was that I have Dependency Behavior in the NuGet options set to 'Lowest', which downloads the lowest compatible versions of dependent packages. Unfortunately this sometimes means it downloads a package version that is not compatible with your project's runtime version. In my case, package System.Net.Http.Formatting.Extensions caused System.Net.Http version 2.20... to be installed, which was not recognized by my .NET 4.7.1 project.

On the Updates tab in the Nuget Package manager for the solution, it will show updates for these packages. Install them and your problem should be solved. Alternatively, set the Dependency Behavior to Highest when installing NuGet packages.

like image 23
Simmetric Avatar answered Feb 06 '23 18:02

Simmetric