diff --git a/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs b/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs index 7bfb6d377a..4c79b94777 100644 --- a/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs +++ b/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Microsoft.AspNetCore.RequestThrottling { - internal class SemaphoreWrapper + internal class SemaphoreWrapper : IDisposable { private SemaphoreSlim _semaphore; @@ -29,5 +29,10 @@ namespace Microsoft.AspNetCore.RequestThrottling { get => _semaphore.CurrentCount; } + + public void Dispose() + { + _semaphore.Dispose(); + } } } diff --git a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs index d6c990bf1f..368804c8cd 100644 --- a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs +++ b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public async Task TracksQueueLength() { - var s = new SemaphoreWrapper(1); + using var s = new SemaphoreWrapper(1); Assert.Equal(1, s.Count); await s.EnterQueue(); @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public void DoesNotWaitIfSpaceAvailible() { - var s = new SemaphoreWrapper(2); + using var s = new SemaphoreWrapper(2); var t1 = s.EnterQueue(); Assert.True(t1.IsCompleted); @@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public async Task WaitsIfNoSpaceAvailible() { - var s = new SemaphoreWrapper(1); + using var s = new SemaphoreWrapper(1); await s.EnterQueue(); var waitingTask = s.EnterQueue(); @@ -56,8 +56,8 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public async Task IsEncapsulated() { - var s1 = new SemaphoreWrapper(1); - var s2 = new SemaphoreWrapper(1); + using var s1 = new SemaphoreWrapper(1); + using var s2 = new SemaphoreWrapper(1); await s1.EnterQueue(); await s2.EnterQueue().TimeoutAfter(TimeSpan.FromSeconds(30));