diff --git a/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs b/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs index 8628904ddb..1943118015 100644 --- a/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs +++ b/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -14,8 +15,8 @@ namespace Microsoft.CodeAnalysis { public static class TestCompilation { - private static Dictionary> _assemblyMetadataReferences = - new Dictionary>(); + private static readonly ConcurrentDictionary> _referenceCache = + new ConcurrentDictionary>(); public static IEnumerable GetMetadataReferences(Assembly assembly) { @@ -40,10 +41,10 @@ namespace Microsoft.CodeAnalysis syntaxTrees = new[] { syntaxTree }; } - if (!_assemblyMetadataReferences.TryGetValue(assembly, out IEnumerable metadataReferences)) + if (!_referenceCache.TryGetValue(assembly, out IEnumerable metadataReferences)) { metadataReferences = GetMetadataReferences(assembly); - _assemblyMetadataReferences[assembly] = metadataReferences; + _referenceCache.TryAdd(assembly, metadataReferences); } var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, metadataReferences);