HttpClient的在ASP.NET 5.0没有发现?发现、ASP、HttpClient、NET

2023-09-02 21:12:40 作者:野性.

使用VS2015和asp.net 5,当我尝试使用System.Net.HttpClient的实例来编译我的网站,它告诉我:

  

类型或命名空间名称'的HttpClient'找不到(你   缺少using指令或程序集引用?)

悬停在违规code,我看到:

 WebApplication1.ASP.NET 5.0  - 可用
WebApplication1.ASP.NET核心5.0  - 不可用
 

我在project.json文件中列出的2框架:

 框架:{
    aspnet50:{},
    aspnetcore50:{}
},
 

我假定这些人负责通过不具有组件,但我真的不知道如何解决它,或者是如何工作的。

我怎样才能让网站与HttpClient的运行引发错误的呢?该问题的方法公布如下:

 专用异步任务<字符串> GetStringFromUri()
{
    使用(VAR的HttpClient =新的HttpClient())
    {
        结果=等待httpClient.GetStringAsync(
        新的URI(http://baconipsum.com/api/?type=meat-and-filler));

        视图模型=结果;
        返回视图模型;
    }
}
 
dotNET 5.0正式发布,新功能尝鲜

解决方案

终于得到它的所有工作了。 @yuval把我在正确的轨道与他有关添加依赖关系,并指出这个类在github上存在的答案。进一步搜索导致我找出这个类似乎并没有被包括在preVIEW释放,只是还没有,我只好这样的NuGet回购添加到我的项目: https://www.myget.org/gallery/aspnetvnext

在该回购是每晚构建的asp.net vnext的NuGet包,其中载有我想要的类。添加以下行到我的主要依赖关系部分和两个框架依赖关系部分得到这个工作对我来说: Microsoft.Net.Http.Client:1.0.0.0-rc1-10049

 依赖:{
    [...]
    Microsoft.Net.Http.Client:1.0.0.0-rc1-10049
},
构架: {
    aspnet50:{
        依赖:{
            Microsoft.Net.Http.Client:1.0.0-rc1-10049
        }
    },
    aspnetcore50:{
        依赖:{
            Microsoft.Net.Http.Client:1.0.0-rc1-10049
        }
    }
}
 

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

解决方案

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