From b24a9e32a1f4ffdd44eae6fd4bc2afaa117c836a Mon Sep 17 00:00:00 2001 From: Shreyas Jejurkar Date: Mon, 14 Mar 2022 11:03:28 +0000 Subject: [PATCH 1/4] Add `AddStackExchangeRedisCache` overload --- .../StackExchangeRedis/src/PublicAPI.Shipped.txt | 1 + ...ackExchangeRedisCacheServiceCollectionExtensions.cs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt index ede022b36341..9c972587d3ef 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt @@ -24,3 +24,4 @@ Microsoft.Extensions.Caching.StackExchangeRedis.RedisCache.Dispose() -> void Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.RedisCacheOptions() -> void Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions +static Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions.AddStackExchangeRedisCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! diff --git a/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs b/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs index 4d2b3c71c224..9bbf64c199ae 100644 --- a/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs +++ b/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs @@ -12,6 +12,16 @@ namespace Microsoft.Extensions.DependencyInjection; /// public static class StackExchangeRedisCacheServiceCollectionExtensions { + /// + /// Adds Redis distributed caching services to the specified . + /// + /// The to add services to. + /// The so that additional calls can be chained. + public static IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services) + { + return AddStackExchangeRedisCache(services, (option) => { }); + } + /// /// Adds Redis distributed caching services to the specified . /// From da3f69e3bba2ff5c9c3d2a578dd6ca61260dc924 Mon Sep 17 00:00:00 2001 From: Shreyas Jejurkar Date: Mon, 14 Mar 2022 11:09:12 +0000 Subject: [PATCH 2/4] Add `AddHsts` overload --- .../HttpsPolicy/src/HstsServicesExtensions.cs | 10 ++++++++++ src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt | 1 + 2 files changed, 11 insertions(+) diff --git a/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs b/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs index 5ccd753c80e2..17ecb59570a0 100644 --- a/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs +++ b/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs @@ -11,6 +11,16 @@ namespace Microsoft.AspNetCore.Builder; /// public static class HstsServicesExtensions { + /// + /// Adds HSTS services. + /// + /// The for adding services. + /// + public static IServiceCollection AddHsts(this IServiceCollection services) + { + return AddHsts(services, (option) => { }); + } + /// /// Adds HSTS services. /// diff --git a/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt b/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt index 25ee5361052e..958bbaf4b2e9 100644 --- a/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt +++ b/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt @@ -41,3 +41,4 @@ static Microsoft.AspNetCore.Builder.HstsBuilderExtensions.UseHsts(this Microsoft static Microsoft.AspNetCore.Builder.HstsServicesExtensions.AddHsts(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configureOptions) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static Microsoft.AspNetCore.Builder.HttpsPolicyBuilderExtensions.UseHttpsRedirection(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder! static Microsoft.AspNetCore.Builder.HttpsRedirectionServicesExtensions.AddHttpsRedirection(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configureOptions) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! +static Microsoft.AspNetCore.Builder.HstsServicesExtensions.AddHsts(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! From 96cb05c6619155b2abd74f631289770216da3d99 Mon Sep 17 00:00:00 2001 From: Shreyas Jejurkar Date: Tue, 15 Mar 2022 06:15:02 +0000 Subject: [PATCH 3/4] Addressed PR feedback --- ...changeRedisCacheServiceCollectionExtensions.cs | 15 ++++++++++++--- .../HttpsPolicy/src/HstsServicesExtensions.cs | 10 ---------- .../HttpsPolicy/src/PublicAPI.Shipped.txt | 1 - 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs b/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs index 9bbf64c199ae..caa478325431 100644 --- a/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs +++ b/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs @@ -4,6 +4,7 @@ using System; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.StackExchangeRedis; +using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.Extensions.DependencyInjection; @@ -19,7 +20,16 @@ public static class StackExchangeRedisCacheServiceCollectionExtensions /// The so that additional calls can be chained. public static IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services) { - return AddStackExchangeRedisCache(services, (option) => { }); + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + + services.AddOptions(); + + services.TryAdd(ServiceDescriptor.Singleton()); + + return services; } /// @@ -41,9 +51,8 @@ public static IServiceCollection AddStackExchangeRedisCache(this IServiceCollect throw new ArgumentNullException(nameof(setupAction)); } - services.AddOptions(); + services.AddStackExchangeRedisCache(); services.Configure(setupAction); - services.Add(ServiceDescriptor.Singleton()); return services; } diff --git a/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs b/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs index 17ecb59570a0..5ccd753c80e2 100644 --- a/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs +++ b/src/Middleware/HttpsPolicy/src/HstsServicesExtensions.cs @@ -11,16 +11,6 @@ namespace Microsoft.AspNetCore.Builder; /// public static class HstsServicesExtensions { - /// - /// Adds HSTS services. - /// - /// The for adding services. - /// - public static IServiceCollection AddHsts(this IServiceCollection services) - { - return AddHsts(services, (option) => { }); - } - /// /// Adds HSTS services. /// diff --git a/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt b/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt index 958bbaf4b2e9..25ee5361052e 100644 --- a/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt +++ b/src/Middleware/HttpsPolicy/src/PublicAPI.Shipped.txt @@ -41,4 +41,3 @@ static Microsoft.AspNetCore.Builder.HstsBuilderExtensions.UseHsts(this Microsoft static Microsoft.AspNetCore.Builder.HstsServicesExtensions.AddHsts(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configureOptions) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static Microsoft.AspNetCore.Builder.HttpsPolicyBuilderExtensions.UseHttpsRedirection(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder! static Microsoft.AspNetCore.Builder.HttpsRedirectionServicesExtensions.AddHttpsRedirection(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configureOptions) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! -static Microsoft.AspNetCore.Builder.HstsServicesExtensions.AddHsts(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! From 787825026b814be5a1acf1497137ee4607578eda Mon Sep 17 00:00:00 2001 From: Shreyas Jejurkar Date: Thu, 14 Apr 2022 07:07:55 +0000 Subject: [PATCH 4/4] Addressed PR feedback. Adds test to conver new extension method. --- .../StackExchangeRedis/src/PublicAPI.Shipped.txt | 1 - .../src/PublicAPI.Unshipped.txt | 1 + .../test/CacheServiceExtensionsTests.cs | 16 ++++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt index 9c972587d3ef..ede022b36341 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt @@ -24,4 +24,3 @@ Microsoft.Extensions.Caching.StackExchangeRedis.RedisCache.Dispose() -> void Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.RedisCacheOptions() -> void Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions -static Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions.AddStackExchangeRedisCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..f84753e4627b 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt @@ -1 +1,2 @@ #nullable enable +static Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions.AddStackExchangeRedisCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! diff --git a/src/Caching/StackExchangeRedis/test/CacheServiceExtensionsTests.cs b/src/Caching/StackExchangeRedis/test/CacheServiceExtensionsTests.cs index d2ceab0c7c35..66529a0737f3 100644 --- a/src/Caching/StackExchangeRedis/test/CacheServiceExtensionsTests.cs +++ b/src/Caching/StackExchangeRedis/test/CacheServiceExtensionsTests.cs @@ -54,4 +54,20 @@ public void AddStackExchangeRedisCache_allows_chaining() Assert.Same(services, services.AddStackExchangeRedisCache(_ => { })); } + + [Fact] + public void AddStackExchangeRedisCache_RegistersRedisCacheWithoutSetupAction() + { + // Arrange + var services = new ServiceCollection(); + + // Act + services.AddStackExchangeRedisCache(); + var distributedCache = services.FirstOrDefault(x => x.ServiceType == typeof(IDistributedCache)); + + // Assert + Assert.NotNull(distributedCache); + Assert.Equal(typeof(RedisCache), distributedCache.ImplementationType); + Assert.Equal(ServiceLifetime.Singleton, distributedCache.Lifetime); + } }