The return type of an async method must be void, Task or Task<T>

Viewed 41227

I have the following code here:

public async Dictionary<string, float> GetLikelihoodsAsync(List<string> inputs)
{
    HttpClient client = new HttpClient(); 

    string uri = GetUri();
    string body = GetRequestBody(inputs);
    byte[] requestData = Encoding.UTF8.GetBytes(body);

    Dictionary<string, float> result = await GetResponseAsync(requestData, client, uri)
        .ContinueWith(responseTask => ParseResponseAsync(responseTask.Result))
        .ContinueWith(task => task.Result.Result);

    return result;
}

with

async Task<HttpResponseMessage> GetResponseAsync(byte[] requestData, HttpClient client, string uri) {...}

async Task<Dictionary<string, float>> ParseResponseAsync(HttpResponseMessage response) {...}

Basically after GetResponseAsync completes I want to take the results and feed it to ParseResponseAsync and get a task with the results of it.

Currently, it gives an compiler error saying

The return type of an async method must be void, Task or Task

What is the best way to achieve this goal and get rid of this error? Other (better solutions) are welcomed and some explanations of why do task.Result.Result in the last ContinueWith are also welcomed.

2 Answers
Related