99using System . Reflection ;
1010using System . Runtime . CompilerServices ;
1111using System . Text . Json ;
12+ using System . Threading ;
1213using System . Threading . Tasks ;
1314
1415[ assembly: InternalsVisibleTo ( "RDMSharpTests" ) ]
@@ -24,6 +25,8 @@ public static class MetadataFactory
2425 private static ConcurrentDictionary < MetadataVersion , List < MetadataJSONObjectDefine > > metadataVersionDefinesBagDictionary ;
2526 private static ConcurrentDictionary < ParameterBag , MetadataJSONObjectDefine > parameterBagDefineCache ;
2627 private static List < Assembly > resourceProvider = new List < Assembly > ( ) { typeof ( MetadataFactory ) . Assembly } ;
28+ public static bool IsInitialized = false ;
29+ private static readonly SemaphoreSlim fillDefaultMetadataVersionListSemaphoreSlim = new SemaphoreSlim ( 1 , 1 ) ;
2730
2831 public static void AddResourceProvider ( Assembly assembly )
2932 {
@@ -58,8 +61,11 @@ public static IReadOnlyCollection<string> GetResources(Assembly assembly)
5861 {
5962 return assembly . GetManifestResourceNames ( ) . Where ( p => p . EndsWith ( JSON_ENDING ) ) . ToList ( ) . AsReadOnly ( ) ;
6063 }
61- private static void fillDefaultMetadataVersionList ( )
64+ private static async Task fillDefaultMetadataVersionList ( )
6265 {
66+ if ( fillDefaultMetadataVersionListSemaphoreSlim . CurrentCount == 0 )
67+ return ;
68+ await fillDefaultMetadataVersionListSemaphoreSlim . WaitAsync ( ) ;
6369 if ( metadataVersionDefinesBagDictionary != null )
6470 return ;
6571
@@ -87,7 +93,7 @@ private static void fillDefaultMetadataVersionList()
8793 {
8894 MaxDegreeOfParallelism = Environment . ProcessorCount , // Optional: Set the maximum degree of parallelism
8995 } ;
90- Parallel . ForEach ( nonSchemaVersions , parallelOptions , mv =>
96+ await Parallel . ForEachAsync ( nonSchemaVersions , parallelOptions , async ( mv , token ) =>
9197 {
9298 try
9399 {
@@ -98,7 +104,8 @@ private static void fillDefaultMetadataVersionList()
98104 versionSchemas . TryAdd ( schema . Version , jsonSchema ) ;
99105 }
100106 MetadataBag metadataBag = new MetadataBag ( mv ) ;
101- var result = jsonSchema . Evaluate ( System . Text . Json . JsonElement . Parse ( metadataBag . Content ) ) ;
107+ var doc = JsonDocument . Parse ( metadataBag . Content ) ;
108+ var result = jsonSchema . Evaluate ( doc . RootElement ) ;
102109 if ( result . IsValid )
103110 {
104111 MetadataJSONObjectDefine jsonDefine = JsonSerializer . Deserialize < MetadataJSONObjectDefine > ( metadataBag . Content ) ;
@@ -121,6 +128,8 @@ private static void fillDefaultMetadataVersionList()
121128 Logger ? . LogError ( $ "Exception while Deserialize { mv . Name } ", e ) ;
122129 }
123130 } ) ;
131+ IsInitialized = true ;
132+ fillDefaultMetadataVersionListSemaphoreSlim . Release ( ) ;
124133 }
125134
126135 public static IReadOnlyCollection < MetadataVersion > GetMetadataSchemaVersions ( )
@@ -466,4 +475,13 @@ public static Type GetDefinedDataTreeObjectType(ERDM_Parameter parameter, Comman
466475 return false ;
467476 } ) . FirstOrDefault ( ) ;
468477 }
478+
479+ public static async Task AwaitInitialize ( )
480+ {
481+ while ( ! IsInitialized )
482+ {
483+ GetMetadataDefineVersions ( ) ;
484+ await Task . Delay ( 100 ) ;
485+ }
486+ }
469487}
0 commit comments