示例#1
0
        public async Task BasicTest()
        {
            var provider = new SqlDistributedSynchronizationProvider(TestingSqlServerDb.DefaultConnectionString);

            const string LockName = TargetFramework.Current + "ProviderBasicTest";

            await using (await provider.AcquireLockAsync(LockName))
            {
                await using var handle = await provider.TryAcquireLockAsync(LockName);

                Assert.IsNull(handle);
            }

            const string ReaderWriterLockName = TargetFramework.Current + "ProviderBasicTest_ReaderWriter";

            await using (await provider.AcquireReadLockAsync(ReaderWriterLockName))
            {
                await using var handle = await provider.TryAcquireWriteLockAsync(ReaderWriterLockName);

                Assert.IsNull(handle);
            }

            const string SemaphoreName = TargetFramework.Current + "ProviderBasicTest_Semaphore";

            await using (await provider.AcquireSemaphoreAsync(SemaphoreName, 2))
            {
                await using var handle = await provider.TryAcquireSemaphoreAsync(SemaphoreName, 2);

                Assert.IsNotNull(handle);

                await using var failedHandle = await provider.TryAcquireSemaphoreAsync(SemaphoreName, 2);

                Assert.IsNull(failedHandle);
            }
        }
示例#2
0
        public void Install(IServiceCollection services, IConfigurationRoot configuration)
        {
            var connectionString = configuration.GetSection("WebApiStarter").GetValue <string>("ConnectionString");
            IDistributedLockProvider provider = new SqlDistributedSynchronizationProvider(connectionString);

            services.AddSingleton(provider);
        }