liveBook · Manning

InvokeAsync

As explained in Pro ASP.NET Core 7, Tenth Edition

An InvokeAsync method is the asynchronous method convention ASP.NET Core calls to execute a view component and obtain its rendered result.

A view component defines InvokeAsync when its work uses asynchronous APIs. The method returns a Task; Razor waits for it to complete before inserting the result into the parent view or Razor Page. A synchronous view component defines Invoke instead. section 24.5.2

Fetch a page size

In Components/PageSize.cs, a ViewComponent defines `public async Task<IViewComponentResult> InvokeAsync()`. It awaits `HttpClient.GetAsync("http://microsoft.com")`, then returns `View(response.Content.Headers.ContentLength)`. The default view at Views/Shared/Components/PageSize/Default.cshtml displays that length. Add `<vc:page-size />` to a controller view to include the result; the calling syntax does not change because the component is asynchronous. section 24.5.2

Listing 24.8 Using a view component in the Index.cshtml file in the Views/Home folder
@model Product?
@{
    Layout = "_Layout";
    ViewBag.Title = "Product Table";
}

@section Header {
    Product Information
}

<tr><th>Name</th><td>@Model?.Name</td></tr>
<tr>
    <th>Price</th>
    <td>@Model?.Price.ToString("c")</td>
</tr>
<tr><th>Category ID</th><td>@Model?.CategoryId</td></tr>

@section Footer {
    @(((Model?.Price / ViewBag.AveragePrice)
            * 100).ToString("F2"))% of average price
}

@section Summary {
    <div class="bg-info text-white m-2 p-2">
        @await Component.InvokeAsync("CitySummary")
    </div>
}

The controller-view listing shows the alternative helper call, `@await Component.InvokeAsync("CitySummary")`. section 24.3.1

Do not confuse the component's InvokeAsync method with `Component.InvokeAsync` in a parent view. The parent always uses that helper method—even when the component itself defines synchronous Invoke. Its first argument names the component; an optional second object supplies values matching the component method's parameters. Missing required values can leave the component absent without an error message. section 24.3.1 section 24.5.1

The name also appears in Blazor, but there it denotes different APIs: a Razor component uses `InvokeAsync` to run a function inside the Blazor environment, while `EventCallback<T>.InvokeAsync` triggers a custom event. Neither is the view-component method that produces content for a parent view. section 35.4.2 section 34.2.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works through an async component, its default view, and its invocation; does not detail Task internals.
Examples
Uses a page-size component that awaits an HTTP GET and renders the reported content length.
What it assumes
Assumes familiarity with C# async and await and Razor views while showing the component class and view to create.
What you can do after
Build it from scratch as an asynchronous view component and include it in a controller view.
How it connects
Connects view components with Razor views, Razor Pages, HttpClient, IViewComponentResult, and the Component.InvokeAsync helper.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.