Blazor InteractiveAuto: IDisposable invoked twice during prerender-to-WASM transition
0 reputation · 01 Feb 2025, 05:26 UTC
Component lifecycle during prerender-to-WebAssembly handoff
In .NET 8+, components rendered with @rendermode InteractiveAuto are instantiated on the server for prerendering, disposed when prerendering completes, then re-instantiated on the client once the WebAssembly runtime loads. This causes IDisposable.Dispose and IAsyncDisposable.DisposeAsync to run on the server before the component ever reaches the browser.
Constraint: no built-in transition signal
The framework exposes no lifecycle event, context property, or service that distinguishes "prerender disposal" from final disposal. Components holding JS interop object references (IJSObjectReference), timers, or native handles cannot reliably determine whether a given Dispose call corresponds to the transient server instance or the long-lived client instance.
Goal
Establish a deterministic pattern for stateful resources that must survive the prerender-to-WASM transition without leaking or double-disposing, while remaining compatible with libraries that do not yet account for InteractiveAuto semantics.
- What is the recommended approach for deferring resource acquisition until after the WebAssembly runtime is active?
- Can a scoped or circuit-scoped service reliably track prerender state across the server/client boundary?
- Are there plans to introduce a lifecycle hook (e.g.,
OnPrerenderComplete) or a context flag in .NET 9 to address this gap?