Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
207 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...