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/src/StackExchangeRedisCacheServiceCollectionExtensions.cs b/src/Caching/StackExchangeRedis/src/StackExchangeRedisCacheServiceCollectionExtensions.cs index 4d2b3c71c224..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; @@ -12,6 +13,25 @@ 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) + { + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + + services.AddOptions(); + + services.TryAdd(ServiceDescriptor.Singleton()); + + return services; + } + /// /// Adds Redis distributed caching services to the specified . /// @@ -31,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/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); + } }