@@ -115,12 +115,10 @@ public RestoringComponentStateSubscription RegisterOnRestoring(Action callback,
115
115
throw new InvalidOperationException ( "Persisting state is only allowed during an OnPersisting callback." ) ;
116
116
}
117
117
118
- if ( _currentState . ContainsKey ( key ) )
118
+ if ( ! _currentState . TryAdd ( key , JsonSerializer . SerializeToUtf8Bytes ( instance , JsonSerializerOptionsProvider . Options ) ) )
119
119
{
120
120
throw new ArgumentException ( $ "There is already a persisted object under the same key '{ key } '") ;
121
121
}
122
-
123
- _currentState . Add ( key , JsonSerializer . SerializeToUtf8Bytes ( instance , JsonSerializerOptionsProvider . Options ) ) ;
124
122
}
125
123
126
124
[ RequiresUnreferencedCode ( "JSON serialization and deserialization might require types that cannot be statically analyzed." ) ]
@@ -133,12 +131,10 @@ internal void PersistAsJson(string key, object instance, [DynamicallyAccessedMem
133
131
throw new InvalidOperationException ( "Persisting state is only allowed during an OnPersisting callback." ) ;
134
132
}
135
133
136
- if ( _currentState . ContainsKey ( key ) )
134
+ if ( ! _currentState . TryAdd ( key , JsonSerializer . SerializeToUtf8Bytes ( instance , type , JsonSerializerOptionsProvider . Options ) ) )
137
135
{
138
136
throw new ArgumentException ( $ "There is already a persisted object under the same key '{ key } '") ;
139
137
}
140
-
141
- _currentState . Add ( key , JsonSerializer . SerializeToUtf8Bytes ( instance , type , JsonSerializerOptionsProvider . Options ) ) ;
142
138
}
143
139
144
140
/// <summary>
@@ -155,12 +151,10 @@ internal void PersistAsBytes(string key, byte[] data)
155
151
throw new InvalidOperationException ( "Persisting state is only allowed during an OnPersisting callback." ) ;
156
152
}
157
153
158
- if ( _currentState . ContainsKey ( key ) )
154
+ if ( ! _currentState . TryAdd ( key , data ) )
159
155
{
160
156
throw new ArgumentException ( $ "There is already a persisted object under the same key '{ key } '") ;
161
157
}
162
-
163
- _currentState . Add ( key , data ) ;
164
158
}
165
159
166
160
/// <summary>
0 commit comments