// 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 Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Redis;
using StackExchange.Redis;
namespace Microsoft.Extensions.DependencyInjection
{
///
/// Extension methods for configuring Redis-based scale-out for a SignalR Server in an .
///
public static class RedisDependencyInjectionExtensions
{
///
/// Adds scale-out to a , using a shared Redis server.
///
/// The .
/// The same instance of the for chaining.
public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder)
{
return AddRedis(builder, o => { });
}
///
/// Adds scale-out to a , using a shared Redis server.
///
/// The .
/// The connection string used to connect to the Redis server.
/// The same instance of the for chaining.
public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder, string redisConnectionString)
{
return AddRedis(builder, o =>
{
o.Configuration = ConfigurationOptions.Parse(redisConnectionString);
});
}
///
/// Adds scale-out to a , using a shared Redis server.
///
/// The .
/// A callback to configure the Redis options.
/// The same instance of the for chaining.
public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder, Action configure)
{
builder.Services.Configure(configure);
builder.Services.AddSingleton(typeof(HubLifetimeManager<>), typeof(RedisHubLifetimeManager<>));
return builder;
}
///
/// Adds scale-out to a , using a shared Redis server.
///
/// The .
/// The connection string used to connect to the Redis server.
/// A callback to configure the Redis options.
/// The same instance of the for chaining.
public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder, string redisConnectionString, Action configure)
{
return AddRedis(builder, o =>
{
o.Configuration = ConfigurationOptions.Parse(redisConnectionString);
configure(o);
});
}
}
}