简体   繁体   English

如何从父类调用异步任务?

[英]How to call async task from parent class?

Today I need your help to debug my code... Indeed, I need to use the Task object to make API calls but it don't work. 今天,我需要您的帮助来调试我的代码...确实,我需要使用Task对象来进行API调用,但是它不起作用。

My RestClient to execute my get / post / put requests: 我的RestClient执行我的get / post / put请求:

public class RestClient
{
    public HttpClient HttpClient { get; set; }

    public RestClient()
    {
        if(HttpClient == null)
        {
            HttpClient = new HttpClient();
        }
    }

    protected async Task<string> Get(string url)
    {
        // Call url in thread
        HttpResponseMessage response = await HttpClient.GetAsync(url);

        // Get content
        var content = await response.Content.ReadAsStringAsync();

        return content;
    }
}

This problem is here, I cannot to get the return of the base.Get(url). 这个问题在这里,我无法获得base.Get(url)的返回。 Do you know how I can fix that ? 你知道我该怎么解决吗?

class CarEnergy : RestClient
{

    public CarEnergy Geter(int id)
    {
        string url = String.Format(Configuration.Configuration.GetCarEnergy, id);

        Debug.WriteLine(url);

        // Wait the get request before continue
        var myTask = base.Get(url); // Impossible to get the return, it bug

        Debug.WriteLine(result);

        var carEnergy = JsonConvert.DeserializeObject<List<CarEnergy>>(result);

        return carEnergy[0];
    }

}

Your Get() method is async , so you have to wait for the result somehow. 您的Get()方法是async ,因此您必须以某种方式等待结果。 The best way to do this is to use await . 最好的方法是使用await That means your Geter() method also needs to be async , and its caller needs to await , and so on. 这意味着您的Geter()方法也需要async ,其调用方需要await ,依此类推。

Note also that the Get() method is neither virtual or hidden, so you don't need to use base to call it. 还要注意, Get()方法既不是虚拟的也不是隐藏的,因此您不需要使用base来调用它。

Fixing the above results in something that looks like this: 解决以上问题,结果如下所示:

public async Task<CarEnergy> Geter(int id)
{
    string url = String.Format(Configuration.Configuration.GetCarEnergy, id);

    Debug.WriteLine(url);

    // Wait the get request before continue
    var myTask = await Get(url);

    Debug.WriteLine(result);

    var carEnergy = JsonConvert.DeserializeObject<List<CarEnergy>>(result);

    return carEnergy[0];
}


You can block on the async method if you must, by simply retrieving the Result property instead of using await : 如果需要,可以通过简单地获取Result属性而不是使用await来阻止async方法:

    var myTask = Get(url).Result;

(In this case, obviously you don't have to change Geter() to be an async method). (在这种情况下,显然您不必将Geter()更改为async方法)。

But I strongly discourage doing that. 但是我强烈不鼓励这样做。 When used correctly, the async / await pattern will wind up woven throughout your code, and will make all of it more UI-friendly and easier to write. 如果使用正确,则async / await模式将在您的整个代码中交织在一起,并使所有这些对用户界面更友好,更易于编写。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM