A Task represents an asynchronous operation; a typed task such as Task<HttpResponseMessage> or Task<long?> produces a result when it completes. section 5.13.1
For HTTP work, ContinueWith attaches code to run after a task completes. The async and await keywords make consuming its result more natural, while the method still returns a Task. ASP.NET Core action methods can return tasks too. section 5.13.1 section 5.13.2
Listing 5.54 Using the async and await keywords in the MyAsyncMethods.cs file in the Models folder
namespace LanguageFeatures.Models {
public class MyAsyncMethods {
public async static Task<long?> GetPageLength() {
HttpClient client = new HttpClient();
var httpMessage = await client.GetAsync("http://manning.com");
return httpMessage.Content.Headers.ContentLength;
}
}
}
See an async method await an HTTP request and return its content length as Task<long?>. section 5.13.2
Listing 5.55 An asynchronous action method in the HomeController.cs file in the Controllers folder
namespace LanguageFeatures.Controllers {
public class HomeController : Controller {
public async Task<ViewResult> Index() {
long? length = await MyAsyncMethods.GetPageLength();
return View(new string[] { $"Length: {length}" });
}
}
}
An action method awaits the helper task before returning a ViewResult. section 5.13.2









