skip to content

Circuits & Persistent State

Blazor state lives in a server circuit or the WebAssembly app, survives prerendering via [PersistentState] and outlives disconnects. Interviewers probe scoped services, per-user memory and lost state.

on this pageshow

explore

questions

6

In a Blazor Web App, where does a component's in-memory state live on Interactive Server and on WebAssembly, and what survives a browser refresh?

level: juniorimportance: must knowfreq 62%

answer

  1. server: circuit memory on the server
  2. WebAssembly: memory of the browser tab
  3. static SSR: nothing between requests
  4. refresh = new circuit or new app
  5. persist what users worked to create

basics

~20 s

On Interactive Server, component fields and circuit-scoped services live in server memory inside the user's circuit; on WebAssembly they live in the browser tab's .NET runtime. A full refresh starts a new circuit or app, so only data saved elsewhere survives.

solid answer

~40 s

Blazor keeps state as ordinary .NET objects: component fields and properties, plus scoped services. On **Interactive Server** they live in the server process inside the user's **circuit**, the per-tab session reached over a SignalR connection. On **Interactive WebAssembly** they live in the .NET runtime running in that browser tab. Navigating between interactive pages keeps those services alive, but a full page refresh, closing the tab or opening a new one creates a new circuit or a new WebAssembly app with freshly initialized state. A statically rendered component keeps nothing between requests. Anything users spend effort on, such as a cake-order basket, must therefore be saved somewhere that outlives the instance: a database, browser storage such as `ProtectedLocalStorage`, or the URL.

code

razor · 13 lines
razor
@page "/order"
@rendermode InteractiveServer

<p>@lines.Count cake(s) in this order</p>
<button @onclick="AddEclair">Add an eclair</button>

@code {
    // Lives in this component instance, inside the user's circuit on the server.
    // A full page refresh creates a new circuit and this list starts empty again.
    private readonly List<string> lines = [];

    private void AddEclair() => lines.Add("Chocolate eclair");
}

go deeper

for a junior

Recall that Interactive Server state is in server memory per circuit, WebAssembly state is in the browser tab, and a refresh loses both.

for a middle

Explain the circuit, why reconnects can still lose state, and which storage fits each kind of state that must survive.

for a senior

Size per-circuit memory, decide what is persisted where, and design for reconnects to a different server in a farm.

for a principal

Choose render modes partly on state cost: server memory per tab versus browser-held state and what each does to scale.

## State is just objects, held somewhere Blazor has no special state store. The state of a page is the values of fields and properties in **component instances** and in **services** resolved from dependency injection. The question is where those objects live, and that depends on how the component is rendered. | Rendering | Where the objects live | How long | |---|---|---| | Static SSR (no render mode) | Server, for one HTTP request | Until the response is written | | Interactive Server | Server, inside the user's **circuit** | Until the circuit ends | | Interactive WebAssembly | The browser tab's .NET runtime | Until the tab unloads the app | | Interactive Auto | Server first, then WebAssembly once downloaded | Per the active runtime | ## The circuit On Interactive Server, each browser tab that runs interactive components gets a **circuit**: the server-side session that holds the component tree, its last render output, the fields of every component instance, and the DI services scoped to that circuit. The browser talks to it over a SignalR connection. User state on the server is therefore kept in server memory, per tab. If the network drops briefly, Blazor tries to reconnect the browser to the same circuit, and the state is still there. If reconnection is impossible, because the server released the circuit, the server restarted, or another server in a farm received the reconnect, the user gets a **new circuit with newly initialized state**, like closing and reopening a desktop app. ## WebAssembly With Interactive WebAssembly, the .NET runtime and the components run inside the browser tab. State lives in that tab's memory. Nothing on the server knows about it, and nothing is lost when the server restarts, but it is gone when the page reloads. ## What a refresh does - **Navigation between interactive pages** inside the app keeps the circuit or WebAssembly app alive, so scoped services such as a basket keep their contents. - **A full reload** (F5, a `forceLoad` navigation, or opening the URL in a new tab) starts a fresh circuit or a fresh WebAssembly app. In-memory state is gone. - **Closing the tab** ends the circuit or app. - Values set in JavaScript variables through JS interop are also lost on reload. ## Deciding what to persist Persistence is never automatic; the app chooses what to keep and where. The documentation's rule of thumb is to persist state users expended effort to create, not data that can simply be read again. 1. **High-value, cross-device state** such as a submitted cake order or a saved basket: store it server-side, typically in a database keyed by the user. 2. **Per-browser convenience state** such as an unsaved basket for an anonymous visitor: browser storage, with `ProtectedLocalStorage` or `ProtectedSessionStorage` on Interactive Server. 3. **Navigation state** such as the selected cake category or page number: the URL, which also survives bookmarks and sharing. 4. **Prerendered data** loaded once on the server: `[PersistentState]` hands it to the interactive instance so it is not loaded twice. UI structure itself, such as component instances and their render trees, cannot be persisted; model the UI state you care about, for example which order panel is expanded, as serializable data. ## Misconceptions to unlearn - **"Blazor Server runs in the browser."** Only the rendered HTML and a small script run there; components and their fields live on the server. - **"Scoped means per user."** On the server it means per circuit, which is per tab; two tabs of the same user have two baskets. - **"Reconnecting always restores everything."** Only while the server still holds the circuit; after that, only state the app explicitly persisted comes back. - **"WebAssembly state is safe from server restarts, so it is durable."** It survives server restarts but not a reload of the tab. - **"Static pages keep their fields between posts."** Each request creates fresh component instances. ## Consequences for server memory Because every connected tab holds its state on the server, per-user memory adds up: a basket that caches full product images for thousands of open tabs is a server capacity problem, not a browser one. Keep circuit state small and load large data on demand.

  • A user's basket survives clicking between pages of the patisserie app but is empty after pressing F5; why?
    In-app navigation between interactive pages keeps the same circuit or WebAssembly app, so a scoped basket service keeps its contents. F5 is a full reload: the browser starts a new circuit or a new WebAssembly app with fresh services. To survive it, save the basket to the server or to browser storage and reload it on startup.
  • Why can server memory become a problem with Interactive Server state even when each user's state is small?
    Each open tab holds a circuit with its component tree and scoped services in server memory, and disconnected circuits are kept for a while in case the user returns. Multiply small per-user state by thousands of tabs and it becomes the server's capacity limit, so circuit state should stay lean.

  • Interactive Server components keep their state in the browser.
  • Component state on Interactive Server survives a full page refresh.
  • Blazor persists component fields automatically between visits.
  • A statically rendered component keeps its fields between form posts.
  • Opening the app in a second tab shares the first tab's circuit.
open as a page

In a Blazor Web App, what does AddScoped mean on Interactive Server and on WebAssembly, and how do you build a shared cake-basket state container safely?

level: middleimportance: must knowfreq 64%

basics

~20 s

On Interactive Server a scoped service lives as long as the user's circuit; in WebAssembly there are no DI scopes, so scoped acts like a singleton for that tab. Register the basket as scoped, raise an event on change, and have components subscribe and unsubscribe.

open as a page

Why does injecting a DbContext directly into Blazor Interactive Server components fail under concurrent use, and how does IDbContextFactory fix it?

level: seniorimportance: must knowfreq 52%

basics

~10 s

AddDbContext registers a scoped DbContext, and on Interactive Server a scope is the whole circuit, so every component shares one non-thread-safe context. Inject IDbContextFactory<TContext> instead and create a short-lived context per operation with CreateDbContextAsync.

open as a page

In Blazor Interactive Server, how do ProtectedLocalStorage and ProtectedSessionStorage keep a cake basket in the browser, and what are their limits?

level: middleimportance: should knowfreq 36%

basics

~20 s

They wrap the browser's localStorage and sessionStorage: SetAsync serializes the value and encrypts it with ASP.NET Core Data Protection, GetAsync decrypts it into a ProtectedBrowserStorageResult<T>. They need JS interop, so they fail during prerendering, and they work only in server-side Blazor.

open as a page

On Blazor Interactive Server, what happens to a user's circuit and cake basket when the connection drops, and what do CircuitOptions and .NET 10 circuit persistence change?

level: seniorimportance: should knowfreq 38%

basics

~20 s

The server keeps a disconnected circuit, by default up to 100 for 3 minutes, while the reconnect UI retries; reconnecting in time loses nothing. In .NET 10, an evicted or paused circuit's [PersistentState] values can resume into a new circuit unless the page reloads.

open as a page

In ASP.NET Core 10 Blazor, how does [PersistentState] stop a prerendered component from loading its data twice, and what code did it replace?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A prerendered component initializes twice: once on the server for HTML, then again when it becomes interactive. [PersistentState] on a public property serializes its prerendered value into the page and restores it before the interactive OnInitializedAsync, so Cakes ??= await Load() skips the second load.

open as a page