In an ASP.NET Core request, an asynchronous operation can give the server more flexibility to schedule other work while it completes. C# uses `async` and `await` to make that operation easier to write without managing a continuation directly. section 5.13 section 5.13.2
A .NET asynchronous operation returns a `Task` representing its eventual result. For example, `HttpClient.GetAsync` returns a `Task<HttpResponseMessage>`. You can attach a `ContinueWith` callback to process the response, but that introduces a lambda and makes the return statements harder to follow. `await` instead gives the next statement the completed result, so the method can read the response and return a value in the usual order. 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;
}
}
}
In `MyAsyncMethods.GetPageLength`, `await client.GetAsync(...)` produces the `HttpResponseMessage`; the method returns its nullable `ContentLength`. Because the method uses `await`, its signature includes `async`, but its declared result remains `Task<long?>`. The caller still receives a task, not an immediate length. 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 ASP.NET Core controller can follow the same pattern. `HomeController.Index` awaits `GetPageLength()`, then passes the resulting length to a view. Its declared return type is `Task<ViewResult>` because the action is asynchronous. section 5.13.2
For a sequence of results, distinguish `await` on a task from `await foreach` over an `IAsyncEnumerable<long?>`. Awaiting a task that produces a collection makes the controller receive that collection after all its HTTP requests complete; `await foreach` lets it receive each sequence value as it is produced. section 5.13.3
















