Skip to content

Commit e02c472

Browse files
committed
Fix SchemaRegistry thread safety (use ConcurrentDictionary)
Dictionary was corrupted by concurrent Get() calls from parallel tests. Replace with ConcurrentDictionary and use GetOrAdd for atomic lookup.
1 parent c8a90cc commit e02c472

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

Data.ORM/SchemaRegistry.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Ecng.Serialization;
22

3+
using System.Collections.Concurrent;
34
using System.Reflection;
45

56
using Ecng.Common;
@@ -9,7 +10,7 @@ namespace Ecng.Serialization;
910
/// </summary>
1011
public static class SchemaRegistry
1112
{
12-
private static readonly Dictionary<Type, Schema> _cache = [];
13+
private static readonly ConcurrentDictionary<Type, Schema> _cache = [];
1314

1415
/// <summary>
1516
/// Registers a schema for the given entity type.
@@ -30,14 +31,7 @@ public static bool TryGet(Type entityType, out Schema meta)
3031
/// Gets the schema for the specified entity type, creating one via reflection if not registered.
3132
/// </summary>
3233
public static Schema Get(Type entityType)
33-
{
34-
if (_cache.TryGetValue(entityType, out var meta))
35-
return meta;
36-
37-
meta = CreateFromReflection(entityType);
38-
_cache[entityType] = meta;
39-
return meta;
40-
}
34+
=> _cache.GetOrAdd(entityType, CreateFromReflection);
4135

4236
private static Schema CreateFromReflection(Type entityType)
4337
{

0 commit comments

Comments
 (0)