简体   繁体   English

使用Web客户端调用时不包含Content.ReadAsStringAsync()的定义

[英]does not contain definition for Content.ReadAsStringAsync() when using web client call

this is my service 这是我的服务

public Task<NewsModel> GetNews(string query,string count)
{
    var task = Task<NewsModel>.Factory
        .StartNew(() => {
            var responseData = "";
            NewsModel list = new NewsModel();
            var client = new HttpClient();
            var uri = "Some URL";
            using (var response = client.GetAsync(uri)) {
                responseData =  response.Content.ReadAsStringAsync();
                list = JsonConvert.DeserializeObject<NewsModel>(responseData);
                list.value.RemoveAll(item => item.image == null);
            }
            return list;

        });

    task.ContinueWith(t => {
    }, TaskContinuationOptions.OnlyOnRanToCompletion);

    task.ContinueWith(t => {
        if (t.Exception == null)
            return;

        var ex = t.Exception.Flatten();

    }, TaskContinuationOptions.OnlyOnFaulted);

    return task;
}

compilation error 编译错误 i am getting the above error when i try to compile can you please tell me how can i use Content.ReadAsStringAsync() in the above functionality 我尝试编译时出现上述错误, 您能告诉我如何在上述功能中使用Content.ReadAsStringAsync()

Those methods return Tasks so they should be awaited. 这些方法返回Tasks,因此应等待它们。

using (var response = await client.GetAsync(uri)) {
    responseData = await response.Content.ReadAsStringAsync();
    list = JsonConvert.DeserializeObject<NewsModel>(responseData);
    list.value.RemoveAll(item => item.image == null);
}

In fact that entire method should be refactored to use async/await 实际上,应将整个方法重构为使用异步/等待

HttpClient client = new HttpClient();
public async Task<NewsModel> GetNews(string query, string count) {
    NewsModel list = new NewsModel();
    var uri = "Some URL";
    using (var response = await client.GetAsync(uri)) {
        var responseData = await response.Content.ReadAsStringAsync();
        list = JsonConvert.DeserializeObject<NewsModel>(responseData);
        list.value.RemoveAll(item => item.image == null);
    }
    return list;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 .Net Core 中的 Content.ReadAsStringAsync 方法获取字符串? - How to get the string from Content.ReadAsStringAsync method in .Net Core? Content.ReadAsStringAsync() json 结果向日期时间属性添加一个额外的小时 - Content.ReadAsStringAsync() json result adding an extra hour to datetime property 使用HttpResponseMessage.Content.ReadAsStringAsync()时没有得到内容的全部; - Not getting the entirety of the content when using HttpResponseMessage.Content.ReadAsStringAsync(); 任务不包含内容的定义 - Task does not contain a definition for Content 方法不包含定义 - Method does not contain definition when it is there 将IdentityUser与OAuth一起使用时,&#39;object&#39;不包含&#39;Action&#39;的定义 - 'object' does not contain a definition for 'Action' when using IdentityUser with OAuth 使用C#从URL获取内容,然后出现此错误“ WebRequest不包含GetRespone和…的定义” - Using C# to get content from URL followed by this error “WebRequest does not contain a definition for GetRespone and …” 使用C#从URL获取内容会收到此错误“ WebRequest不包含GetRespone和…的定义” - Using C# get content from URL get this error “WebRequest does not contain a definition for GetRespone and …” “对象不包含针对 <field> ”使用反射 - “object does not contain a definition for <field>” using reflection “尽管使用System.Web.Odata.Extensions,&#39;&#39;HttpRequestMessageProperties&#39;不包含&#39;PathHandler&#39;的定义” - “'HttpRequestMessageProperties' does not contain a definition for 'PathHandler'” despite using System.Web.Odata.Extensions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM