christok
christok

Reputation: 1107

The type or namespace 'HttpClient' could not be found

I am losing my mind over this reference error. I've added the Microsoft.Net.Http Nuget package, made sure the System.Net.Http reference is added to the page, imported the System.Net.Http namespace to the class. Nothing. I even went as far as to recreate the project from scratch using a blank template.

Can someone offer some ideas? Thanks!

Upvotes: 22

Views: 57682

Answers (9)

Adam Jonsson
Adam Jonsson

Reputation: 2244

For people that are updating to .NET 8, a breaking change is that:
"Implicit using for System.Net.Http no longer added".

So you need to explicitly use the namespace:

using System.Net.Http

Read more about the change here:
https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/implicit-global-using-netfx

Upvotes: 2

st_stefanov
st_stefanov

Reputation: 1186

Use the fully qualified name:

public void MyMethod()
{
    System.Net.Http.HttpClient client = ....
}

Instead of:

using System.Net.Http
public class MyClass
{
    public void MyMethod()
    {
        HttpClient client = ...
    }
}

This should show you what issue you are having. If it is name conflict...

Upvotes: 8

Strider489
Strider489

Reputation: 67

Make sure your build is .net 4.5 and up. I was running .net 4.0 and that library will not work. You need to move your solution to 4.5. Then install with Nuget.

Upvotes: 0

InsideTrack
InsideTrack

Reputation: 241

Not sure if you have already tried this, but:

All I did was add the System.Net.Http in the project references.

In the Project Explorer, right click references -> add reference, then tick the relevant checkbox from the list of dlls and click OK

Upvotes: 24

RSK
RSK

Reputation: 2211

Since you have already added the Microsoft.Net.Http Nuget package, there must be an exclamation mark next to System.Net.Http in your project references as below: enter image description here

You need to remove this and again: right click it and select 'Remove' and now add it again from the list of dlls

Upvotes: 0

LeFish
LeFish

Reputation: 43

In my case the solution was to upgrade Visual Studio Professional 2017 from 15.x.y to 15.9.18.

Upvotes: 0

Ryan
Ryan

Reputation: 521

If targetting 4.6.2 or above, ignore whatever nuget packages are installed as long as they are installed because they are dependencies of your existing references.

Then, open the .csproj files that reference the dll and remove any specific version association so the reference now looks like this (older csproj format):

<Reference Include="System.Net.Http" />

Previously, my config looked something like this:

<Reference Include="System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll</HintPath>
  <Private>True</Private>
  <Private>True</Private>
</Reference>

This will strongly encourage msbuild to use the version in the GAC for the targetted framework. In my case I was targetting 4.7.2 and it used that version when it warned of a conflict in versions due to the nuget packages.

Upvotes: 11

Alvin George
Alvin George

Reputation: 14296

Step 1:

Right click on your namespace ie, project -> Add -> Add Nuget Packages.

Step 2:

Search for system.net.http and Add Package.

Step 3:

Add using System.Net.Http; to your cs class.

Upvotes: 3

frans eilering
frans eilering

Reputation: 499

In my visual studio 2017 Xamarin.forms project I had the same problem. If I right click my solution, there is the "Manage NuGet packages..." If I select it, in the "Browse", I look for "System.net" I selected the found item, enter image description here checked the checkboxes for the project and clicked "Install"

Upvotes: 12

Related Questions