Skip to content

Blazor persistent services: document singleton registration requirement for WebAssembly clients - #37412

Merged
guardrex merged 3 commits into
mainfrom
copilot/serialize-state-for-services
Aug 5, 2026
Merged

Blazor persistent services: document singleton registration requirement for WebAssembly clients#37412
guardrex merged 3 commits into
mainfrom
copilot/serialize-state-for-services

Conversation

Copilot AI commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

The "Serialize state for services" section only showed a scoped server registration, which led users to register the service as scoped on the .Client project as well—causing a DirectScopedResolvedFromRootException at runtime under InteractiveAuto/InteractiveWebAssembly render modes.

Root cause: ComponentStatePersistenceManager is a singleton on the client and resolves dependencies from the root service provider. A scoped registration throws in Development, and even without the exception, produces two separate instances—so restored state never reaches the component's injected instance.

Changes to prerendered-state-persistence.md:

  • Qualified the existing note — "Only persisting scoped services is supported" now explicitly scopes that statement to the server; flags that the WebAssembly client requires a singleton.
  • Split the Program file example into two subsections:
    • Server project service registration — unchanged AddScoped + RegisterPersistentService example, with context clarified to "server project."
    • Client project service registration — new subsection with the required singleton registration:
      // .Client project Program.cs
      builder.Services.AddSingleton<CounterTracker>();
      Explains why singleton is required (root-provider resolution, single shared instance).

Internal previews

File Preview link
aspnetcore/blazor/state-management/prerendered-state-persistence.md aspnetcore/blazor/state-management/prerendered-state-persistence

…vices

Fixes #37411: Updates the "Serialize state for services" section to clarify
that for InteractiveAuto and InteractiveWebAssembly render modes, the service
must be registered as a singleton in the .Client project's Program.cs.
Adds "Server project service registration" and "Client project service
registration" subsections with clear examples and explains why singleton
registration is required on the client.
Copilot AI changed the title [WIP] Update documentation for WebAssembly service registration Blazor persistent services: document singleton registration requirement for WebAssembly clients Aug 3, 2026
Copilot AI requested a review from guardrex August 3, 2026 13:42
Comment thread aspnetcore/blazor/state-management/prerendered-state-persistence.md Outdated
@guardrex

guardrex commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

@copilot ... Accept my suggestion for Line 221.

Copilot AI requested a review from guardrex August 3, 2026 13:54
@guardrex
guardrex marked this pull request as ready for review August 3, 2026 13:54

Copilot AI commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

@copilot ... Accept my suggestion for Line 221.

That change was already applied in 9b7d98a — line 221 matches the suggestion exactly.

@guardrex guardrex left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oroztocil ... AI did a nice job with this one. If you approve, we can merge this now. It won't let me approve/merge it without another review after I've done anything with Copilot due to the new restrictive merging rules 💸😩.

@oroztocil

Copy link
Copy Markdown
Member

@oroztocil ... AI did a nice job with this one. If you approve, we can merge this now. It won't let me approve/merge it without another review after I've done anything with Copilot due to the new restrictive merging rules 💸😩.

The update looks good. Let me confirm with @javiercn that we want to use this as official recommendation, or proceed otherwise (e.g. change the implementation to not require this approach).

@javiercn javiercn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@guardrex
guardrex merged commit cabe379 into main Aug 5, 2026
6 checks passed
@guardrex
guardrex deleted the copilot/serialize-state-for-services branch August 5, 2026 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serialize state for services: document that WebAssembly clients must register the service as a singleton (Auto/WebAssembly render modes)

4 participants