What's wrong with this async Task method?

2019-02-10 07:20发布

问题:

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

回答1:

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