public static void AddSignalR(this IServiceCollection services, ApplicationConfiguration applicationConfiguration) { // register SignalR ISignalRServerBuilder signalRBuilder = services.AddSignalR(configure: options => { options.EnableDetailedErrors = applicationConfiguration.Environment.IsLocalDevelopmentOrTest(); }) .AddJsonProtocol(configure: options => { JsonSerializerOptions serializerSettings = options.PayloadSerializerOptions; serializerSettings.IgnoreNullValues = true; serializerSettings.PropertyNameCaseInsensitive = false; serializerSettings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; serializerSettings.WriteIndented = false; JsonConverterSetup.Configure(serializerSettings.Converters); // Note Additional converters that require DI are enabled elsewhere }); // if we're running anywhere but LOCAL or TEST then configure the redis backplane for SignalR // DON'T ATTEMPT TO ENABLE THIS LOCALLY, IT WILL NOT WORK. AWS redis does not allow connections // from outside AWS (regardless of security group configuration) if (!applicationConfiguration.Environment.IsLocalOrTest()) { _signalRConnectionFactory = new SignalRConnectionFactory(); signalRBuilder.AddStackExchangeRedis(configure: options => { options.Configuration.ChannelPrefix = $"Labs-NFT-Server-{applicationConfiguration.Environment.GetName()}"; options.ConnectionFactory = writer => _signalRConnectionFactory.ConnectionFactoryAsync(writer: writer, applicationConfiguration: applicationConfiguration); }); } }
public static ISignalRServerBuilder AddRedisBackplaneForPopForums(this ISignalRServerBuilder signalRServerBuilder) { var serviceProvider = signalRServerBuilder.Services.BuildServiceProvider(); var config = serviceProvider.GetService <IConfig>(); signalRServerBuilder.AddStackExchangeRedis(config.CacheConnectionString); return(signalRServerBuilder); }
public static void AddRedisBackplane(this ISignalRServerBuilder builder, IConfiguration configuration) { var redisConnectionString = configuration.GetConnectionString("RedisConnectionString"); if (!string.IsNullOrEmpty(redisConnectionString)) { builder.AddStackExchangeRedis(redisConnectionString, options => options.Configuration.ChannelPrefix = configuration["SignalR:RedisBackplane:ChannelName"] ?? "VirtoCommerceChannel"); } }
public static ISignalRServerBuilder AddPushNotifications(this ISignalRServerBuilder builder, IConfiguration configuration) { builder.Services.AddSingleton <IPushNotificationStorage, PushNotificationInMemoryStorage>(); var scalabilityMode = configuration["PushNotifications:ScalabilityMode"]; builder.Services.AddOptions <PushNotificationOptions>().Bind(configuration.GetSection("PushNotifications")).ValidateDataAnnotations(); // SignalR scalability configuration. if (scalabilityMode != null && !scalabilityMode.EqualsInvariant("None")) { //Enable to store in the json the full type information to be able deserialize a push notifications types on the other instances builder.AddNewtonsoftJsonProtocol(jsonOptions => { jsonOptions.PayloadSerializerSettings.TypeNameHandling = TypeNameHandling.Objects; jsonOptions.PayloadSerializerSettings.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple; }); builder.Services.AddSingleton <IPushNotificationManager, ScalablePushNotificationManager>(); builder.Services.AddHostedService <PushNotificationSynchronizerTask>(); if (scalabilityMode.EqualsInvariant("AzureSignalRService")) { var azureSignalRConnectionString = configuration["PushNotifications:AzureSignalRService:ConnectionString"]; if (string.IsNullOrEmpty(azureSignalRConnectionString)) { throw new InvalidOperationException($"PushNotifications:AzureSignalRService:ConnectionString must be set"); } builder.AddAzureSignalR(o => { o.Endpoints = new[] { new ServiceEndpoint(azureSignalRConnectionString) }; }); } else { var redisConnectionString = configuration.GetConnectionString("RedisConnectionString"); var redisChannelName = configuration["PushNotifications:RedisBackplane:ChannelName"]; redisChannelName = string.IsNullOrEmpty(redisChannelName) ? "VirtoCommerceChannel" : redisChannelName; if (string.IsNullOrEmpty(redisConnectionString)) { throw new InvalidOperationException($"RedisConnectionString must be set"); } builder.AddStackExchangeRedis(redisConnectionString, o => o.Configuration.ChannelPrefix = redisChannelName); } } else { builder.Services.AddSingleton <IPushNotificationManager, PushNotificationManager>(); } return(builder); }
/// <summary> /// Extension для инициализации SignalR через Redis /// </summary> /// <param name="serverBuilder"></param> /// <param name="options">Настройки подключения Redis</param> /// <returns></returns> public static ISignalRServerBuilder AddRedisBackPlane(this ISignalRServerBuilder serverBuilder, RedisOptions options) { var serviceProvider = serverBuilder.Services.BuildServiceProvider(); var logger = serviceProvider.GetService <ILogger>(); serverBuilder .AddStackExchangeRedis( o => { o.ConnectionFactory = async writer => { ConnectionMultiplexer connection = null; try { var config = ConfigurationOptions.Parse(options.Configuration); config.AbortOnConnectFail = false; connection = await ConnectionMultiplexer.ConnectAsync(config, writer); } catch (Exception ex) { logger.Fatal("Ошибка инициализации Redis", ex); } connection.ConnectionFailed += (_, e) => { logger.Error($"Ошибка подключения к Redis. Configuration: {connection.Configuration}", e.Exception); }; if (!connection.IsConnected) { logger.Error($"Ошибка подкючения к Redis. Configuration: {connection.Configuration}"); } return(connection); }; }); return(serverBuilder); }
/// <summary> /// ÅäÖ÷þÎñ /// </summary> public void ConfigureServices(IServiceCollection services) { //ÅäÖÿçÓò services.AddCors(options => options.AddPolicy(typeof(Startup).FullName, builder => { builder.AllowAnyMethod() .AllowAnyHeader() .SetIsOriginAllowed(_ => true) .AllowCredentials(); })); //ÅäÖÃSignalR ISignalRServerBuilder signalRServerBuilder = services.AddSignalR(options => { options.EnableDetailedErrors = true; }); signalRServerBuilder.AddStackExchangeRedis(options => { options.ConnectionFactory = _ => Task.Run(() => RedisManager.Instance as IConnectionMultiplexer); }); }