Skip to content

PersistentState doesn't work on non-public properties #63268

@AndreiGorlov

Description

@AndreiGorlov

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Persistent state not persisting if [PersistentState] applied to non-public property

Expected Behavior

PersistentStateAttribute should work with private properties

Steps To Reproduce

Counter page:

@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @CurrentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    [PersistentState]
    private int? CurrentCount { get; set; }

    protected override void OnInitialized()
    {
        CurrentCount ??= Random.Shared.Next(100);
    }

    private void IncrementCount()
    {
        CurrentCount++;
    }
}

Exceptions (if any)

log error when navigate to /counter page:

fail: Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager[1000]
      There was an error executing a callback while pausing the application.
      System.InvalidOperationException: Property CurrentCount not found on type BlazorPersistentStatePlayground.Client.Pages.Counter
         at Microsoft.AspNetCore.Components.Infrastructure.PersistentStateValueProvider.PropertyGetterFactory(ValueTuple`2 key)
         at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
         at Microsoft.AspNetCore.Components.Infrastructure.PersistentStateValueProvider.ResolvePropertyGetter(Type type, String propertyName)
         at Microsoft.AspNetCore.Components.Infrastructure.PersistentStateValueProvider.<>c__DisplayClass13_0.<Subscribe>b__0()
         at Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.<TryPauseAsync>g__TryExecuteCallback|18_0(Func`1 callback, ILogger`1 logger)

.NET Version

10.0.0-preview.7.25380.108

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions