Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with this async Task method?

This is just a simple async task but I have always got strange compiler errors. This code from a Web API service in a ASP.NET 4 project, created with VS2010.

Even ContinueWith (non-generic) returns Task implicitly but this error still exists.

Code:

public class TestController : ApiController
{
       public Task<HttpResponseMessage> Test()
       {
            string url = "http://www.stackoverflow.com";
            var client = new HttpClient();

            return client.GetAsync(url).ContinueWith<HttpResponseMessage>((request) =>
            {
                // Error 361 'System.Threading.Tasks.Task' does not contain a definition
                // for 'Result' and no extension method 'Result' accepting a first argument
                // of type 'System.Threading.Tasks.Task' could be found
                // (are you missing a using directive or an assembly reference?)
                var response = request.Result;
                response.EnsureSuccessStatusCode();

                // Error 364 Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'System.Net.Http.HttpResponseMessage'
                return response.Content.ReadAsStringAsync().ContinueWith<HttpResponseMessage>((read) =>
                {
                    return new HttpResponseMessage();
                });
            });
        }
}
like image 226
Tien Do Avatar asked Mar 21 '12 06:03

Tien Do


Video Answer


1 Answers

The 364 error is perfectly normal because you are returning a Task<Task<HttpResponseMessage>> instead of Task<HttpResponseMessage>. Once you fix that the 361 error will also disappear.

So you could Unwrap the result:

public Task<HttpResponseMessage> Test()
{
    string url = "http://www.stackoverflow.com";
    var client = new HttpClient();
    return client.GetAsync(url).ContinueWith(request =>
    {
        var response = request.Result;
        response.EnsureSuccessStatusCode();
        return response.Content.ReadAsStringAsync().ContinueWith(t =>
        {
            var result = new HttpResponseMessage();
            response.CreateContent(t.Result);
            return response;
        });
    }).Unwrap();
}
like image 129
Darin Dimitrov Avatar answered Sep 25 '22 16:09

Darin Dimitrov