dotnet runtime_error ai_generated true

System.Threading.Tasks.TaskCanceledException: 任务已取消。在Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost..ctor处

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

ID: dotnet/blazor-wasm-prerendering-timeout

其他格式: JSON · Markdown 中文 · English
85%修复率
88%置信度
1证据数
2023-06-10首次发现

版本兼容性

版本状态引入弃用备注
.NET 6.0 active
.NET 7.0 active
.NET 8.0 active
Blazor Server 6.0+ active
Blazor WASM 6.0+ active

根因分析

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

English

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

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

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

    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% 失败

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