@@ -30,7 +30,8 @@ public abstract class AzureSession : IAzureSession
30
30
static IAzureSession _instance ;
31
31
static bool _initialized = false ;
32
32
static ReaderWriterLockSlim sessionLock = new ReaderWriterLockSlim ( LockRecursionPolicy . SupportsRecursion ) ;
33
- private IDictionary < ComponentKey , object > _componentRegistry = new ConcurrentDictionary < ComponentKey , object > ( new ComponentKeyComparer ( ) ) ;
33
+ // explicit typing for the thread-safe API calls to avoid the need for explicit casting
34
+ private ConcurrentDictionary < ComponentKey , object > _componentRegistry = new ConcurrentDictionary < ComponentKey , object > ( new ComponentKeyComparer ( ) ) ;
34
35
private event EventHandler < AzureSessionEventArgs > _eventHandler ;
35
36
36
37
/// <summary>
@@ -89,7 +90,7 @@ public abstract class AzureSession : IAzureSession
89
90
public string OldProfileFile { get ; set ; }
90
91
91
92
/// <summary>
92
- /// The directory contianing the ARM ContextContainer
93
+ /// The directory containing the ARM ContextContainer
93
94
/// </summary>
94
95
public string ARMProfileDirectory { get ; set ; }
95
96
@@ -214,13 +215,16 @@ public static void Modify(Action<IAzureSession> modifier)
214
215
public bool TryGetComponent < T > ( string componentName , out T component ) where T : class
215
216
{
216
217
var key = new ComponentKey ( componentName , typeof ( T ) ) ;
217
- component = null ;
218
- if ( _componentRegistry . ContainsKey ( key ) )
218
+ if ( _componentRegistry . TryGetValue ( key , out var componentObj ) && componentObj is T componentT )
219
219
{
220
- component = _componentRegistry [ key ] as T ;
220
+ component = componentT ;
221
+ return true ;
222
+ }
223
+ else
224
+ {
225
+ component = null ;
226
+ return false ;
221
227
}
222
-
223
- return component != null ;
224
228
}
225
229
226
230
public void RegisterComponent < T > ( string componentName , Func < T > componentInitializer ) where T : class
@@ -234,16 +238,16 @@ public void RegisterComponent<T>(string componentName, Func<T> componentInitiali
234
238
( ) =>
235
239
{
236
240
var key = new ComponentKey ( componentName , typeof ( T ) ) ;
237
- if ( ! _componentRegistry . ContainsKey ( key ) || overwrite )
241
+ if ( ! _componentRegistry . ContainsKey ( key ) || overwrite ) // only proceed if key not found or overwrite is true
238
242
{
239
- if ( _componentRegistry . ContainsKey ( key ) && overwrite )
243
+
244
+ if ( overwrite
245
+ && _componentRegistry . TryGetValue ( key , out var existed )
246
+ && existed is IAzureSessionListener existedListener )
240
247
{
241
- var existed = _componentRegistry [ key ] ;
242
- if ( existed is IAzureSessionListener existedListener )
243
- {
244
- _eventHandler -= existedListener . OnEvent ;
245
- }
248
+ _eventHandler -= existedListener . OnEvent ;
246
249
}
250
+
247
251
var component = componentInitializer ( ) ;
248
252
_componentRegistry [ key ] = component ;
249
253
if ( component is IAzureSessionListener listener )
@@ -260,14 +264,9 @@ public void UnregisterComponent<T>(string componentName) where T : class
260
264
( ) =>
261
265
{
262
266
var key = new ComponentKey ( componentName , typeof ( T ) ) ;
263
- if ( _componentRegistry . ContainsKey ( key ) )
267
+ if ( _componentRegistry . TryRemove ( key , out var component ) && component is IAzureSessionListener listener )
264
268
{
265
- var component = _componentRegistry [ key ] ;
266
- if ( component is IAzureSessionListener listener )
267
- {
268
- _eventHandler -= listener . OnEvent ;
269
- }
270
- _componentRegistry . Remove ( key ) ;
269
+ _eventHandler -= listener . OnEvent ;
271
270
}
272
271
} ) ;
273
272
}
0 commit comments