Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor (Server) and the async/await pattern

The Introduction to ASP.NET Core Blazor article by Microsoft (Daniel Roth and Luke Latham) show examples of awaited calls in the Razor Code, e.g.

@code {
    private WeatherForecast[] forecasts;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }
}

However, none of the examples suggest whether the razor pages should continue on the captured context, or not, e.g.

.ConfigureAwait(false);
or 
.ConfigureAwait(true); 

Does Blazor have the concept of the UI Thread being the only thread able to update components? What is considered "best practice" when calling awaited calls in the Page and/or in page components.

like image 856
DrGriff Avatar asked Jan 31 '26 03:01

DrGriff


1 Answers

Does Blazor have the concept of the UI Thread being the only thread able to update components?

With Server-side Blazor: yes.
With Client-side there only is one thread (JavaScript) so, yes, kind of.

What is considered "best practice" when calling awaited calls in the Page and/or in page components.

To not use any ConfigureAwait().

You are on a sync context that has an implicit default behaviour of ConfigureAwait(true).

ConfigureAwait() will only be useful when your create extra threads (with Task.Run()) but in general you shouldn't want that.

like image 50
Henk Holterman Avatar answered Feb 03 '26 07:02

Henk Holterman