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

- **ID:** `dotnet/blazor-wasm-prerendering-timeout`
- **领域:** dotnet
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| .NET 6.0 | active | — | — |
| .NET 7.0 | active | — | — |
| .NET 8.0 | active | — | — |
| Blazor Server 6.0+ | active | — | — |
| Blazor WASM 6.0+ | active | — | — |

## 解决方案

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.
   ```

## 无效尝试

- **Increasing the Kestrel request timeout globally in appsettings.json** — The timeout is specific to Blazor circuit creation, not the HTTP request pipeline, so Kestrel settings have no effect. (60% 失败率)
- **Adding a try-catch around the entire OnInitializedAsync method to swallow the exception** — The circuit creation is already canceled before the catch block executes, so the component fails to render. (25% 失败率)
