public void Create_CanReturnConnectionMultiplexer() { // arrange RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceName = "instanceId", AbortOnConnectFail = false, ConnectTimeout = 1 }; RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "127.0.0.1", 4321, "sipassword") { ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" } }; // act var factory = new RedisServiceConnectorFactory(si, config, typeof(ConnectionMultiplexer), typeof(ConfigurationOptions), RedisTypeLocator.StackExchangeInitializer); var multi = factory.Create(null); // assert Assert.NotNull(multi); Assert.IsType <ConnectionMultiplexer>(multi); }
public void Create_CanReturnRedisCache() { // arrange RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceName = "instanceId" }; RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword") { ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" } }; // act var factory = new RedisServiceConnectorFactory(si, config, typeof(RedisCache), typeof(RedisCacheOptions), null); var cache = factory.Create(null); // assert Assert.NotNull(cache); Assert.IsType <RedisCache>(cache); }
public void Create_CanReturnConnectionMultiplexer() { // arrange RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceName = "instanceId", AbortOnConnectFail = false, ConnectTimeout = 1 }; RedisServiceInfo si = new RedisServiceInfo("myId", "127.0.0.1", 4321, "sipassword") { ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" } }; MethodInfo initializer = typeof(ConnectionMultiplexer).GetMethod("Connect", new Type[] { typeof(ConfigurationOptions), typeof(TextWriter) }); // act var factory = new RedisServiceConnectorFactory(si, config, typeof(ConnectionMultiplexer), typeof(ConfigurationOptions), initializer); var multi = factory.Create(null); // assert Assert.NotNull(multi); Assert.IsType <ConnectionMultiplexer>(multi); }
/// <summary> /// Adds ConnectionMultiplexer (as ConnectionMultiplexer and IConnectionMultiplexer) to your Autofac Container /// </summary> /// <param name="container">Your Autofac Container Builder</param> /// <param name="config">Application configuration</param> /// <param name="serviceName">Cloud Foundry service name binding</param> /// <returns>the RegistrationBuilder for (optional) additional configuration</returns> public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterRedisConnectionMultiplexer( this ContainerBuilder container, IConfiguration config, string serviceName = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } Type redisInterface = RedisTypeLocator.StackExchangeInterface; Type redisImplementation = RedisTypeLocator.StackExchangeImplementation; Type redisOptions = RedisTypeLocator.StackExchangeOptions; MethodInfo initializer = RedisTypeLocator.StackExchangeInitializer; RedisServiceInfo info = serviceName != null ? config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName) : config.GetSingletonServiceInfo <RedisServiceInfo>(); RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config); RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null); container.Register(c => new RedisHealthContributor(factory, redisImplementation, c.ResolveOptional <ILogger <RedisHealthContributor> >())).As <IHealthContributor>(); return(container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation)); }
/// <summary> /// Adds ConnectionMultiplexer (as ConnectionMultiplexer and IConnectionMultiplexer) to your Autofac Container /// </summary> /// <param name="container">Your Autofac Container Builder</param> /// <param name="config">Application configuration</param> /// <param name="serviceName">Cloud Foundry service name binding</param> /// <returns>the RegistrationBuilder for (optional) additional configuration</returns> public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterRedisConnectionMultiplexer( this ContainerBuilder container, IConfiguration config, string serviceName = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } Type redisInterface = RedisTypeLocator.StackExchangeRedisInterface; Type redisImplementation = RedisTypeLocator.StackExchangeRedisImplementation; Type redisOptions = RedisTypeLocator.StackExchangeRedisOptions; MethodInfo initializer = RedisTypeLocator.StackExchangeInitializer; if (redisInterface == null || redisImplementation == null || redisOptions == null || initializer == null) { throw new ConnectorException("Unable to find required Redis types, are you missing a StackExchange.Redis Nuget Package?"); } RedisServiceInfo info; if (serviceName != null) { info = config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName); } else { info = config.GetSingletonServiceInfo <RedisServiceInfo>(); } RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config); RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null); return(container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation)); }
/// <summary> /// Adds RedisCache (as IDistributedCache and RedisCache) to your Autofac Container /// </summary> /// <param name="container">Your Autofac Container Builder</param> /// <param name="config">Application configuration</param> /// <param name="serviceName">Cloud Foundry service name binding</param> /// <returns>the RegistrationBuilder for (optional) additional configuration</returns> public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterDistributedRedisCache( this ContainerBuilder container, IConfiguration config, string serviceName = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } Type redisInterface = RedisTypeLocator.MicrosoftRedisInterface; Type redisImplementation = RedisTypeLocator.MicrosoftRedisImplementation; Type redisOptions = RedisTypeLocator.MicrosoftRedisOptions; if (redisInterface == null || redisImplementation == null || redisOptions == null) { throw new ConnectorException("Unable to find required Redis types, are you missing the Microsoft.Extensions.Caching.Redis Nuget package?"); } RedisServiceInfo info; if (serviceName != null) { info = config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName); } else { info = config.GetSingletonServiceInfo <RedisServiceInfo>(); } RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config); RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, null); return(container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation)); }