diff --git a/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs index b52330a5cb..824899d73e 100644 --- a/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs @@ -4,6 +4,7 @@ using System; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Redis; +using StackExchange.Redis; namespace Microsoft.Extensions.DependencyInjection { @@ -14,6 +15,14 @@ namespace Microsoft.Extensions.DependencyInjection return AddRedis(builder, o => { }); } + public static ISignalRBuilder AddRedis(this ISignalRBuilder builder, string redisConnectionString) + { + return AddRedis(builder, o => + { + o.Options = ConfigurationOptions.Parse(redisConnectionString); + }); + } + public static ISignalRBuilder AddRedis(this ISignalRBuilder builder, Action configure) { builder.Services.Configure(configure); diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisDependencyInjectionExtensionsTests.cs b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisDependencyInjectionExtensionsTests.cs new file mode 100644 index 0000000000..785048e5a2 --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisDependencyInjectionExtensionsTests.cs @@ -0,0 +1,41 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Xunit; + +namespace Microsoft.AspNetCore.SignalR.Redis.Tests +{ + public class RedisDependencyInjectionExtensionsTests + { + // No need to go too deep with these tests, or we're just testing StackExchange.Redis again :). It's the one doing the parsing. + [Theory] + [InlineData("testredis.example.com", "testredis.example.com", 0, null, false)] + [InlineData("testredis.example.com:6380,ssl=True", "testredis.example.com", 6380, null, true)] + [InlineData("testredis.example.com:6380,password=hunter2,ssl=True", "testredis.example.com", 6380, "hunter2", true)] + public void AddRedisWithConnectionStringProperlyParsesOptions(string connectionString, string host, int port, string password, bool useSsl) + { + var services = new ServiceCollection(); + services.AddSignalR().AddRedis(connectionString); + var provider = services.BuildServiceProvider(); + + var options = provider.GetService>(); + Assert.NotNull(options.Value); + Assert.NotNull(options.Value.Options); + Assert.Equal(password, options.Value.Options.Password); + Assert.Collection(options.Value.Options.EndPoints, + endpoint => + { + var dnsEndpoint = Assert.IsType(endpoint); + Assert.Equal(host, dnsEndpoint.Host); + Assert.Equal(port, dnsEndpoint.Port); + }); + Assert.Equal(useSsl, options.Value.Options.Ssl); + } + } +}