dotnet/aspnetcore · error · ArgumentException
There is already a persisted object under the same key
Error message
There is already a persisted object under the same key '{key}' What it means
Thrown by the public PersistAsJson<T> when _currentState.TryAdd(key, ...) returns false, i.e. the same key has already been persisted in this cycle. Keys are unique within a persist pass to avoid one component silently overwriting another's data. The duplicate is rejected so the conflict surfaces immediately.
Solutions
- Namespace keys per component or feature: use $"{GetType().FullName}:state" or a constant unique to the module.
- Ensure only one component owns each persisted key; coordinate ownership in code review.
- If multiple values are legitimately needed, combine them into one persisted object or use distinct keys.
- Use TryTakeFromJson on restore with the exact same namespaced key.
Example fix
// before: two components share the literal "data" key
state.PersistAsJson("data", _value);
// after: scope the key to the owning component
state.PersistAsJson($"{GetType().FullName}:data", _value); Defensive patterns
Strategy: validation
Validate before calling
static string ScopedKey(object owner, string key) => $"{owner.GetType().FullName}:{key}";
// then always pass ScopedKey(this, "data") Try / catch
try { state.PersistAsJson(key, instance); }
catch (ArgumentException ex) when (ex.Message.Contains("already a persisted object under the same key"))
{ /* use a more specific key, or merge payloads */ } Prevention
- Namespace persisted keys by component or feature.
- Centralize key constants and review for duplicates in code review.
- Dispose the PersistingComponentStateSubscription when a component unmounts to avoid stale registrations.
When it happens
Trigger: Two components (or two persist callbacks) both call PersistAsJson with the same key during one persist phase; a single component registers two persist callbacks that write the same key; a key constant reused across feature modules.
Common situations: Shared key constants (e.g. "state") used by multiple components; a component rendered multiple times in the same tree each registering persistence under a fixed key; refactoring that merged two persistence modules without namespacing their keys.
Related errors
- existingState
- PersistentComponentState already initialized.
- Persisting state is only allowed during an OnPersisting…
- Registering a callback while persisting state is not…
- State already persisted.
AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11).
Data as JSON: /api/errors/4723e02a9df0abb3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Components/Components/src/PersistentComponentState.cs:120
/// <summary>
/// Serializes <paramref name="instance"/> as JSON and persists it under the given <paramref name="key"/>.
/// </summary>
/// <typeparam name="TValue">The <paramref name="instance"/> type.</typeparam>
/// <param name="key">The key to use to persist the state.</param>
/// <param name="instance">The instance to persist.</param>
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
public void PersistAsJson<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(string key, TValue instance)
{
ArgumentNullException.ThrowIfNull(key);
if (!PersistingState)
{
throw new InvalidOperationException("Persisting state is only allowed during an OnPersisting callback.");
}
if (!_currentState.TryAdd(key, JsonSerializer.SerializeToUtf8Bytes(instance, JsonSerializerOptionsProvider.Options)))
{
throw new ArgumentException($"There is already a persisted object under the same key '{key}'");
}
}
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
internal void PersistAsJson(string key, object instance, [DynamicallyAccessedMembers(JsonSerialized)] Type type)
{
ArgumentNullException.ThrowIfNull(key);
if (!PersistingState)
{
throw new InvalidOperationException("Persisting state is only allowed during an OnPersisting callback.");
}
if (!_currentState.TryAdd(key, JsonSerializer.SerializeToUtf8Bytes(instance, type, JsonSerializerOptionsProvider.Options)))
{
throw new ArgumentException($"There is already a persisted object under the same key '{key}'");
}
}View on GitHub (pinned to 3600ca084e)