HttpClient in ASP.NET 5.0 not found?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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

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

Method 5

Just add this to your project.json

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

or whatever the latest version is.

Method 6

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)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x