Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Scoped service state resets randomly after async JS interop + navigation. is this a DI lifetime or rendering issue?

$
0
0

I'm facing a non-deterministic state loss issue in a Blazor WebAssembly (.NET 6) app that only happens after a specific sequence of async JS interop + navigation + re-render.

Scenario

  • Blazor WebAssembly (.NET 6)
  • Hosted model
  • Scoped service used as a client-side state container
  • Async JS interop call (IJSRuntime.InvokeAsync)
  • Navigation via NavigationManager.NavigateTo
  • Components use OnInitializedAsync and OnAfterRenderAsync

Problem

A scoped service randomly loses its state after navigation only when:

  1. A JS interop call is awaited
  2. Followed by navigation
  3. And a component re-renders during the same render cycle

This does NOT happen consistently, which makes it extremely hard to debug.

Service:

public class AppState{    public Guid SessionId { get; set; } = Guid.NewGuid();    public string? CurrentUser { get; set; }}

Registered as:

builder.Services.AddScoped<AppState>();

Component A:

@inject AppState AppState@inject IJSRuntime JS@inject NavigationManager Nav<button @onclick="HandleClick">Go</button>@code {    private async Task HandleClick()    {        await JS.InvokeVoidAsync("console.log", AppState.SessionId);        // After this navigation, AppState is SOMETIMES reset        Nav.NavigateTo("/pageB");    }}

Component B:

@inject AppState AppState<p>Session: @AppState.SessionId</p>@code {    protected override void OnInitialized()    {        // Sometimes a NEW Guid appears here        Console.WriteLine(AppState.SessionId);    }}

Observed behavior:

  • SessionId should remain constant
  • Occasionally, a new Guid is generated, as if:
    • The scoped service was recreated
    • Or the DI scope was reset
  • No page reload occurs
  • No forceLoad: true
  • Happens more frequently when:
    • Network throttling is enabled
    • Browser tab loses focus
    • Rendering is heavy (Virtualize / large DOM)

What I've verified:

  • ✔ No AddTransient
  • ✔ No forceLoad navigation
  • ✔ No page refresh
  • ✔ Same browser tab
  • ✔ No manual service recreation
  • ✔ Happens only in WebAssembly (not Server)

Questions

  1. Under what internal conditions does Blazor WebAssembly recreate the DI scope?
  2. Can async JS interop cause a render boundary that resets scoped services?
  3. Is this related to the single-threaded WebAssembly synchronization context?
  4. Is there a known issue where navigation during await causes service re-instantiation?
  5. What is the correct pattern for guaranteed client-side state persistence in Blazor WASM?

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>