Skip to content

PersistentComponentState in Maui Blazor #43833

Closed
1 of 1 issue completed
Closed
Bug
1 of 1 issue completed
@mstancombe

Description

@mstancombe

Description

When maui blazor renders a component that injects the PersistentComponentState, it fails.

This documentation shows how to use persisted state in a component.
https://docs.microsoft.com/en-us/aspnet/core/blazor/components/prerendering-and-integration?view=aspnetcore-6.0&pivots=server#persist-prerendered-state

When a component that injects PersistentComponentState is placed into a blazor server or wasm library, it works, as the server library adds it here:

services.TryAddScoped<PersistentComponentState>(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);

And the wasm adds it in the builder here:

Services.AddSingleton<PersistentComponentState>(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);

However, this doesn't appear to be added in Maui anywhere, and due to this, when maui blazor includes a component that injects the PersistentComponentState, it will cause an error.

Manually adding the registrations during maui building resolves this, as per workaround.

Steps to Reproduce

  1. Clone the repro
  2. Try run MauiApp2. First page will show yellow error bar
  3. Uncomment lines 23/24 of MauiProgram.cs
  4. Run it again, and it will now work

Link to public reproduction project repository

https://github.com/mstancombe/MauiBlazorPersistantStateIssueRepro

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows

Affected platform versions

Windows 11

Did you find any workaround?

Using the same registration as the WASM builder does in MauiProgram resolves this:

image

Relevant log output

No response

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pillar: Technical DebtPriority:3Work that is nice to havearea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.feature-blazor-hybridhelp candidateIndicates that the issues may be a good fit for community to help with. Requires work from eng. team

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions