-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Hi, Is the PersistentState
attribute supposed to work for private properties? There's no warning in VS nor any guidance on how it should behave for private properties unless I've missed anything.
@page "/counter"
@using Microsoft.AspNetCore.Components.QuickGrid
@rendermode InteractiveServer
<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; } = 0;
protected override void OnInitialized()
{
if (!RendererInfo.IsInteractive)
{
currentCount = 5;
}
}
private void IncrementCount()
{
currentCount++;
}
}
It throws the following exception at runtime
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 BlazorApp.Net10.Components.Pages.Counter
at Microsoft.AspNetCore.Components.Infrastructure.PersistentStateValueProvider.PropertyGetterFactory(ValueTuple2 key) at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func2 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)
Expected Behavior
-
If private properties aren't supported, then the error message should be clear enough for the developer to fix the issue
There should also be a code analyzer warning in place and the documentation should state this explicitly as anyone would easily fall into this trap before realizing -
If it's a supported use case, then there shouldn't be any exception and the value should be persisted and restored successfully for private properties
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
10.0.100-preview.7.25368.105
Anything else?
No response