dotnet runtime_error ai_generated true

System.Threading.Tasks.TaskCanceledException: A task was canceled. at Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost..ctor

ID: dotnet/blazor-wasm-prerendering-timeout

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2023-06-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
.NET 6.0 active
.NET 7.0 active
.NET 8.0 active
Blazor Server 6.0+ active
Blazor WASM 6.0+ active

Root Cause

Blazor Server or WASM prerendering times out because the component's OnInitializedAsync method performs a long-running operation (e.g., database call or HTTP request) that exceeds the 30-second default timeout.

generic

中文

Blazor Server或WASM预渲染超时,因为组件的OnInitializedAsync方法执行了超过30秒默认超时的长时间运行操作(例如数据库调用或HTTP请求)。

Official Documentation

https://learn.microsoft.com/en-us/aspnet/core/blazor/components/prerendering?view=aspnetcore-8.0

Workarounds

  1. 80% success Configure the Blazor circuit timeout by setting 'CircuitOptions.DisconnectedCircuitRetentionPeriod' and 'CircuitOptions.JSInteropDefaultCallTimeout' in Program.cs to a higher value (e.g., 60 seconds).
    Configure the Blazor circuit timeout by setting 'CircuitOptions.DisconnectedCircuitRetentionPeriod' and 'CircuitOptions.JSInteropDefaultCallTimeout' in Program.cs to a higher value (e.g., 60 seconds).
  2. 90% success Move long-running operations out of OnInitializedAsync and use OnAfterRenderAsync with a loading state to fetch data after the circuit is established.
    Move long-running operations out of OnInitializedAsync and use OnAfterRenderAsync with a loading state to fetch data after the circuit is established.

中文步骤

  1. Configure the Blazor circuit timeout by setting 'CircuitOptions.DisconnectedCircuitRetentionPeriod' and 'CircuitOptions.JSInteropDefaultCallTimeout' in Program.cs to a higher value (e.g., 60 seconds).
  2. Move long-running operations out of OnInitializedAsync and use OnAfterRenderAsync with a loading state to fetch data after the circuit is established.

Dead Ends

Common approaches that don't work:

  1. Increasing the Kestrel request timeout globally in appsettings.json 60% fail

    The timeout is specific to Blazor circuit creation, not the HTTP request pipeline, so Kestrel settings have no effect.

  2. Adding a try-catch around the entire OnInitializedAsync method to swallow the exception 25% fail

    The circuit creation is already canceled before the catch block executes, so the component fails to render.