add AddRedis overload that takes connection string (#1588)

This commit is contained in:
Andrew Stanton-Nurse 2018-03-14 16:24:28 -07:00 committed by GitHub
parent c5d38ae32a
commit 2b68db873a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -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<RedisOptions> configure)
{
builder.Services.Configure(configure);

View File

@ -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<IOptions<RedisOptions>>();
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<DnsEndPoint>(endpoint);
Assert.Equal(host, dnsEndpoint.Host);
Assert.Equal(port, dnsEndpoint.Port);
});
Assert.Equal(useSsl, options.Value.Options.Ssl);
}
}
}