diff --git a/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheInterceptor.csproj b/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheInterceptor.csproj index 5f64803..40ddef3 100644 --- a/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheInterceptor.csproj +++ b/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheInterceptor.csproj @@ -63,7 +63,7 @@ - + diff --git a/src/Tests/EFCoreSecondLevelCacheInterceptor.Tests/Settings/EFServiceProvider.cs b/src/Tests/EFCoreSecondLevelCacheInterceptor.Tests/Settings/EFServiceProvider.cs index 0eac255..d3600d2 100644 --- a/src/Tests/EFCoreSecondLevelCacheInterceptor.Tests/Settings/EFServiceProvider.cs +++ b/src/Tests/EFCoreSecondLevelCacheInterceptor.Tests/Settings/EFServiceProvider.cs @@ -1,7 +1,7 @@ -using System; +using System; using System.IO; -using System.Threading; using System.Threading.Tasks; +using AsyncKeyedLock; using CacheManager.Core; using EasyCaching.Core.Configurations; using EasyCaching.InMemory; @@ -54,7 +54,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s public static class EFServiceProvider { - private static readonly SemaphoreSlim _semaphoreSlim = new(1, 1); + private static readonly AsyncNonKeyedLocker _locker = new(); public static IEFCacheServiceProvider GetCacheServiceProvider(TestCacheProvider provider) { @@ -377,8 +377,7 @@ public static string GetConnectionString(string basePath, IConfigurationRoot con bool cacheAllQueries, params Action[] actions) { - _semaphoreSlim.Wait(); - try + using (_locker.Lock()) { var serviceProvider = GetConfiguredContextServiceProvider(cacheProvider, logLevel, cacheAllQueries); var cacheServiceProvider = serviceProvider.GetRequiredService(); @@ -394,10 +393,6 @@ public static string GetConnectionString(string basePath, IConfigurationRoot con } } } - finally - { - _semaphoreSlim.Release(); - } } public static async Task RunInContextAsync( @@ -406,8 +401,7 @@ public static string GetConnectionString(string basePath, IConfigurationRoot con bool cacheAllQueries, params Func[] actions) { - await _semaphoreSlim.WaitAsync(); - try + using (await _locker.LockAsync()) { var serviceProvider = GetConfiguredContextServiceProvider(cacheProvider, logLevel, cacheAllQueries); var cacheServiceProvider = serviceProvider.GetRequiredService(); @@ -424,10 +418,6 @@ public static string GetConnectionString(string basePath, IConfigurationRoot con } } } - finally - { - _semaphoreSlim.Release(); - } } public static void ExecuteInParallel(Action test, int count = 40)