22

Using VS2015 and asp.net 5, when I try to compile my site using an instance of System.Net.HttpClient, it tells me:

The type or namespace name 'HttpClient' could not be found (are you missing a using directive or an assembly reference?)

Hovering over the offending code, I see:

"WebApplication1.ASP.NET 5.0 - Available"
"WebApplication1.ASP.NET Core 5.0 - Not Available"

I have 2 frameworks listed in my project.json file:

"frameworks": {
    "aspnet50": { },
    "aspnetcore50": { }
},

I'm assuming that one of these is responsible by not having the assembly, but I don't really know how to fix it or how this works.

How can I get the site to run with HttpClient instead of throwing errors? The offending method posted below:

private async Task<string> GetStringFromUri()
{
    using (var httpClient = new HttpClient())
    {
        result = await httpClient.GetStringAsync(
        new Uri("http://baconipsum.com/api/?type=meat-and-filler"));

        viewModel= result;
        return viewModel;
    }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
damccull
  • 3,908
  • 2
  • 17
  • 22

6 Answers6

15

Finally got it all worked out. @yuval set me on the right track with his answer about adding dependencies and pointing out that the class exists on github. Further searching led me to figure out that the class doesn't seem to be included in the preview release just yet, and I had to add this nuget repo to my project: https://www.myget.org/gallery/aspnetvnext

In that repo are nightly builds of the asp.net vnext nuget packages, which contained the class I want. Adding the following line to my main dependencies section and to both frameworks dependencies sections got this working for me: "Microsoft.Net.Http.Client": "1.0.0.0-rc1-10049"

"dependencies": {
    [...],
    "Microsoft.Net.Http.Client": "1.0.0.0-rc1-10049"
},
"frameworks": {
    "aspnet50": {
        "dependencies": {
            "Microsoft.Net.Http.Client": "1.0.0-rc1-10049"
        }
    },
    "aspnetcore50": {
        "dependencies": {
            "Microsoft.Net.Http.Client": "1.0.0-rc1-10049"
        }
    }
} 
damccull
  • 3,908
  • 2
  • 17
  • 22
  • Make sure to add the https://www.myget.org/gallery/aspnetvnext nuget package source first in the list or else it won't work. Only way I figured that out was by unchecking all other package sources. I used the Manage Nuget Packages gui to install it and it only added the dependency to the top level "dependencies" and not to the framework dependencies. I'm using Visual Studio 2015 CTP6. – Philip Holly Apr 07 '15 at 18:27
  • Also HttpClient is in System.Net.Http. The strange thing is when I removed Microsoft.Net.Http.Client and tried to add System.Net.Http (4.0.0-beta from aspnetvnext) I could see it in the Dependencies tree in the Solution Explorer but HttpClient was missing. Installing Microsoft.Net.Http.Client, which has a dependency on System.Net.Http 4.0.0-beta works though. – Philip Holly Apr 07 '15 at 20:07
  • Latest version is now 1.0.0-beta3-10053 – Stef Heyenrath Apr 19 '15 at 17:59
  • How do you configure this to work with Azure? When deploying the site Azure doesn't know to look in the new package source and fails with "Unable to locate Microsoft.Net.Http.Client >= 1.0.0-beta8-15501". I can't seem to find anywhere where it describes how to do this for Azure: https://github.com/aspnet/Home/wiki/Configuring-the-feed-used-by-dnu-to-restore-packages – Marchy Oct 11 '15 at 04:19
  • 2
    This is an outdated answer – Boris Lipschitz Dec 30 '15 at 04:54
  • @BorisLipschitz I just used this today to fix a problem in Visual Studio 2015 Enterprise that is fully up to date. – krillgar Feb 03 '16 at 19:17
11

I've encountered the same problem today and the solution has gotten somewhat simpler in 2016.

It isn't required to add a new Nuget repository any longer.

When adding System.Net.Http (current version 4.0.1-beta-23516), you will still get the same error if you are targeting a clr as well as a core framework version, so need to copy the dependency into the dnxcore framework dependency list, to get your code compiling.

Before:

"frameworks": {
    "dnx451": { 
      "dependencies": {
        "System.Net.Http": "4.0.1-beta-23516"
      }
     },
    "dnxcore50": { }
  }

After:

"frameworks": {
    "dnx451": {
      "dependencies": {
        "System.Net.Http": "4.0.1-beta-23516"
      }
    },
    "dnxcore50": {
      "dependencies": {
        "System.Net.Http": "4.0.1-beta-23516"
      }
    }
  }
Marco
  • 22,856
  • 9
  • 75
  • 124
6

You need to add a new source to your nuget package manager (https://www.myget.org/F/aspnetvnext) and then add dependencies to your project.json file. Both for dnx451 and dnxcore50:

{
   "frameworks": {
       "dnx451": {
         "frameworkAssemblies": {
             "Microsoft.Net.Http.Client": "1.0.0-beta3-10053"
              }
         },
         "dnxcore50": {
            "frameworkAssemblies": {
               "Microsoft.Net.Http.Client": "1.0.0-beta3-10053"
               }
          }
 }

It is implemented as part of the ASP.Net xNext package, as they state on github:

Fully managed HttpMessageHandler implementation based on sockets.

This project is part of ASP.NET vNext. You can find samples, documentation and getting started instructions for ASP.NET vNext at the Home repo.

A full walkthrough can be found here

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Thanks for the help with this. I'll try it later tonight. Would you mind explaining how I can find dependencies like this for c#6 since it doesn't seem to have docs live on Microsoft's websites yet? – damccull Jan 15 '15 at 01:43
  • @damccull I'd suggest looking at the [ASP.NET vNext dev branch](https://www.myget.org/gallery/aspnetvnext) – Yuval Itzchakov Jan 15 '15 at 06:58
  • 1
    Thanks a ton. As soon as i get a chance to try this code I'll mark this as the answer. – damccull Jan 15 '15 at 12:38
  • Turns out I have the same message after putting in the dependencies like that. It's like HttpClient no longer exists, or hasn't been written for the new system yet. Any other suggestions? My ultimate goal is to download some json from a url. – damccull Jan 15 '15 at 19:25
  • After searching I found this to be the new dependency name: "netfx-System.Net.Http.HttpEntityClient": "1.2.0" – Steven Mar 28 '15 at 11:32
  • 1
    this is an outdated answer – Boris Lipschitz Dec 30 '15 at 05:15
4

I had trouble finding in RC1 today:

For anyone who finds this question from there, HttpClient is now in the System.Net.Http namespace.

Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
3

Just add this to your project.json

"dependencies": {
    ...
    "Microsoft.Net.Http": "2.2.7-beta"
},

or whatever the latest version is.

Alkasai
  • 3,757
  • 1
  • 19
  • 25
  • Which framework are you targeting? I added the reference yet is not found. – user3285954 Oct 30 '15 at 15:21
  • @user3285954 at the time we're on DNX beta 8, and I don't see us referencing the Microsoft.Net.Http library explicitly. But back on beta 4 we used `"Microsoft.Net.Http": "2.2.7-beta"` – Alkasai Nov 02 '15 at 14:29
0

There was a missing link in this thread for me .... you need to add the https://www.myget.org/gallery/aspnetvnext NuGet source, but the actual NuGet feed URL for that is 'https://www.myget.org/F/aspnetvnext/'. Once you have that as your nugget source, the rest of it comes to life.

Also, at the time of writing (17th June 2015), the version is "Microsoft.Net.Http.Client": "1.0.0-beta3-10053".

You'll also need to use the updated 'frameworks' section from Matt DeKreys edit on June 7th (dnx451 and dnxcore50)

Martin Kearn
  • 2,313
  • 1
  • 21
  • 35