private Connection GetConnection(RabbitMQServiceInfo info, IConfiguration configuration) { var rabbitConfig = new RabbitMQProviderConnectorOptions(configuration); var configurer = new RabbitMQProviderConfigurer(); return(new Connection(configurer.Configure(info, rabbitConfig), "RabbitMQ", info)); }
/// <summary> /// Adds RabbitMQ ConnectionFactory (as IConnectionFactory and ConnectionFactory) 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> RegisterRabbitMQConnection(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 rabbitMQInterfaceType = RabbitMQTypeLocator.IConnectionFactory; Type rabbitMQImplementationType = RabbitMQTypeLocator.ConnectionFactory; RabbitMQServiceInfo info = serviceName == null ? config.GetSingletonServiceInfo <RabbitMQServiceInfo>() : config.GetRequiredServiceInfo <RabbitMQServiceInfo>(serviceName); RabbitMQProviderConnectorOptions rabbitMQConfig = new RabbitMQProviderConnectorOptions(config); RabbitMQProviderConnectorFactory factory = new RabbitMQProviderConnectorFactory(info, rabbitMQConfig, rabbitMQImplementationType); container.Register(c => new RabbitMQHealthContributor(factory, c.ResolveOptional <ILogger <RabbitMQHealthContributor> >())).As <IHealthContributor>(); return(container.Register(c => factory.Create(null)).As(rabbitMQInterfaceType, rabbitMQImplementationType)); }
internal void UpdateConfiguration(RabbitMQServiceInfo si, RabbitMQProviderConnectorOptions configuration) { if (si == null) { return; } if (!string.IsNullOrEmpty(si.Uri)) { if (si.Scheme.Equals(RabbitMQProviderConnectorOptions.Default_SSLScheme, System.StringComparison.OrdinalIgnoreCase)) { configuration.SslEnabled = true; configuration.SslPort = si.Port; } else { configuration.Port = si.Port; } if (configuration.UrlEncodedCredentials) { configuration.Username = WebUtility.UrlDecode(si.UserName); configuration.Password = WebUtility.UrlDecode(si.Password); } else { configuration.Username = si.UserName; configuration.Password = si.Password; } configuration.Server = si.Host; configuration.VirtualHost = si.Path; } }
public void Constructor_ThrowsIfConfigNull() { RabbitMQProviderConnectorOptions config = null; RabbitMQServiceInfo si = null; var ex = Assert.Throws <ArgumentNullException>(() => new RabbitMQProviderConnectorFactory(si, config, typeof(ConnectionFactory))); Assert.Contains(nameof(config), ex.Message); }
public RabbitMQProviderConnectorFactory(RabbitMQServiceInfo sinfo, RabbitMQProviderConnectorOptions config, Type connectFactory) { _config = config ?? throw new ArgumentNullException(nameof(config)); ConnectorType = connectFactory ?? throw new ArgumentNullException(nameof(connectFactory)); _info = sinfo; _setUri = FindSetUriMethod(ConnectorType); if (_setUri == null) { throw new ConnectorException("Unable to find ConnectionFactory.SetUri(), incompatible RabbitMQ assembly"); } }
private static void DoAdd(IServiceCollection services, RabbitMQServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime) { Type rabbitMQInterfaceType = RabbitMQTypeLocator.IConnectionFactory; Type rabbitMQImplementationType = RabbitMQTypeLocator.ConnectionFactory; RabbitMQProviderConnectorOptions rabbitMQConfig = new RabbitMQProviderConnectorOptions(config); RabbitMQProviderConnectorFactory factory = new RabbitMQProviderConnectorFactory(info, rabbitMQConfig, rabbitMQImplementationType); services.Add(new ServiceDescriptor(rabbitMQInterfaceType, factory.Create, contextLifetime)); services.Add(new ServiceDescriptor(rabbitMQImplementationType, factory.Create, contextLifetime)); services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RabbitMQHealthContributor(factory, ctx.GetService <ILogger <RabbitMQHealthContributor> >()), ServiceLifetime.Singleton)); }
public void Is_Connected_Returns_Up_Status() { var rabbitMQImplementationType = RabbitMQTypeLocator.ConnectionFactory; var rabbitMQConfig = new RabbitMQProviderConnectorOptions(); var sInfo = new RabbitMQServiceInfo("MyId", "amqp://localhost:5672"); var logrFactory = new LoggerFactory(); var connFactory = new RabbitMQProviderConnectorFactory(sInfo, rabbitMQConfig, rabbitMQImplementationType); var h = new RabbitMQHealthContributor(connFactory, logrFactory.CreateLogger <RabbitMQHealthContributor>()); var status = h.Health(); Assert.Equal(HealthStatus.UP, status.Status); Assert.Contains("version", status.Details.Keys); }
private static void DoAdd(IServiceCollection services, RabbitMQServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, bool addSteeltoeHealthChecks) { var rabbitMQInterfaceType = RabbitMQTypeLocator.IConnectionFactory; var rabbitMQImplementationType = RabbitMQTypeLocator.ConnectionFactory; var rabbitMQConfig = new RabbitMQProviderConnectorOptions(config); var factory = new RabbitMQProviderConnectorFactory(info, rabbitMQConfig, rabbitMQImplementationType); services.Add(new ServiceDescriptor(rabbitMQInterfaceType, factory.Create, contextLifetime)); services.Add(new ServiceDescriptor(rabbitMQImplementationType, factory.Create, contextLifetime)); if (!services.Any(s => s.ServiceType == typeof(HealthCheckService)) || addSteeltoeHealthChecks) { services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RabbitMQHealthContributor(factory, ctx.GetService <ILogger <RabbitMQHealthContributor> >()), ServiceLifetime.Singleton)); } }
public void Not_Connected_Returns_Down_Status() { _ = RabbitMQTypeLocator.IConnectionFactory; var rabbitMQImplementationType = RabbitMQTypeLocator.ConnectionFactory; var rabbitMQConfig = new RabbitMQProviderConnectorOptions(); var sInfo = new RabbitMQServiceInfo("MyId", "amqp://*****:*****@localhost:5672/si_vhost"); var logrFactory = new LoggerFactory(); var connFactory = new RabbitMQProviderConnectorFactory(sInfo, rabbitMQConfig, rabbitMQImplementationType); var h = new RabbitMQHealthContributor(connFactory, logrFactory.CreateLogger <RabbitMQHealthContributor>()); var status = h.Health(); Assert.Equal(HealthStatus.DOWN, status.Status); Assert.Equal("Failed to open RabbitMQ connection!", status.Description); }
private static void DoAdd(IServiceCollection services, RabbitMQServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime) { Type rabbitMQInterfaceType = ConnectorHelpers.FindType(rabbitMQAssemblies, rabbitMQInterfaceTypeNames); Type rabbitMQImplementationType = ConnectorHelpers.FindType(rabbitMQAssemblies, rabbitMQImplementationTypeNames); if (rabbitMQInterfaceType == null || rabbitMQImplementationType == null) { throw new ConnectorException("Unable to find ConnectionFactory, are you missing RabbitMQ assembly"); } RabbitMQProviderConnectorOptions rabbitMQConfig = new RabbitMQProviderConnectorOptions(config); RabbitMQProviderConnectorFactory factory = new RabbitMQProviderConnectorFactory(info, rabbitMQConfig, rabbitMQImplementationType); services.Add(new ServiceDescriptor(rabbitMQInterfaceType, factory.Create, contextLifetime)); services.Add(new ServiceDescriptor(rabbitMQImplementationType, factory.Create, contextLifetime)); }
public void Create_ReturnsRabbitMQConnection() { var config = new RabbitMQProviderConnectorOptions() { Server = "localhost", Port = 5672, Password = "******", Username = "******", VirtualHost = "vhost" }; var si = new RabbitMQServiceInfo("MyId", "amqp://*****:*****@example.com:5672/si_vhost"); var factory = new RabbitMQProviderConnectorFactory(si, config, typeof(ConnectionFactory)); var connection = factory.Create(null); Assert.NotNull(connection); }
/// <summary> /// Add RabbitMQ to a ServiceCollection /// </summary> /// <param name="services">Service collection to add to</param> /// <param name="config">App configuration</param> /// <param name="contextLifetime">Lifetime of the service to inject</param> /// <param name="logFactory">logger factory</param> /// <returns>IServiceCollection for chaining</returns> public static IServiceCollection AddRabbitMQConnection(this IServiceCollection services, IConfiguration config, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ILoggerFactory logFactory = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } RabbitMQServiceInfo info = config.GetSingletonServiceInfo <RabbitMQServiceInfo>(); DoAdd(services, info, config, contextLifetime); return(services); }
public void Is_Connected_Returns_Up_Status() { // arrange Type rabbitMQInterfaceType = RabbitMQTypeLocator.IConnectionFactory; Type rabbitMQImplementationType = RabbitMQTypeLocator.ConnectionFactory; RabbitMQProviderConnectorOptions rabbitMQConfig = new RabbitMQProviderConnectorOptions(); var sInfo = new RabbitMQServiceInfo("MyId", "amqp://localhost:5672"); var logrFactory = new LoggerFactory(); var connFactory = new RabbitMQProviderConnectorFactory(sInfo, rabbitMQConfig, rabbitMQImplementationType); var h = new RabbitMQHealthContributor(connFactory, logrFactory.CreateLogger <RabbitMQHealthContributor>()); // act var status = h.Health(); // assert Assert.Equal(HealthStatus.UP, status.Status); }
public void ConnectionTypeLocatorFindsTypeFromServiceInfo() { var cosmosInfo = new CosmosDbServiceInfo("id"); var mongoInfo = new MongoDbServiceInfo("id", "mongodb://host"); var mysqlInfo = new MySqlServiceInfo("id", "mysql://host"); var oracleInfo = new OracleServiceInfo("id", "oracle://host"); var postgresInfo = new PostgresServiceInfo("id", "postgres://host"); var rabbitMqInfo = new RabbitMQServiceInfo("id", "rabbitmq://host"); var redisInfo = new RedisServiceInfo("id", "redis://host"); var sqlInfo = new SqlServerServiceInfo("id", "sqlserver://host"); var manager = new ConnectionStringManager(new ConfigurationBuilder().Build()); Assert.StartsWith("CosmosDb", manager.GetFromServiceInfo(cosmosInfo).Name); Assert.StartsWith("MongoDb", manager.GetFromServiceInfo(mongoInfo).Name); Assert.StartsWith("MySql", manager.GetFromServiceInfo(mysqlInfo).Name); Assert.StartsWith("Oracle", manager.GetFromServiceInfo(oracleInfo).Name); Assert.StartsWith("Postgres", manager.GetFromServiceInfo(postgresInfo).Name); Assert.StartsWith("RabbitMQ", manager.GetFromServiceInfo(rabbitMqInfo).Name); Assert.StartsWith("Redis", manager.GetFromServiceInfo(redisInfo).Name); Assert.StartsWith("SqlServer", manager.GetFromServiceInfo(sqlInfo).Name); }
public void UpdateConfiguration_WithRabbitMQSSLServiceInfo_UpdatesConfigurationFromServiceInfo() { RabbitMQProviderConfigurer configurer = new RabbitMQProviderConfigurer(); RabbitMQProviderConnectorOptions config = new RabbitMQProviderConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", VirtualHost = "vhost" }; RabbitMQServiceInfo si = new RabbitMQServiceInfo("MyId", "amqps://*****:*****@example.com:5671/si_vhost"); configurer.UpdateConfiguration(si, config); Assert.True(config.SslEnabled); Assert.Equal("example.com", config.Server); Assert.Equal(5671, config.SslPort); Assert.Equal("si_username", config.Username); Assert.Equal("si_password", config.Password); Assert.Equal("si_vhost", config.VirtualHost); }
/// <summary> /// Add RabbitMQ and its IHealthContributor to a ServiceCollection /// </summary> /// <param name="services">Service collection to add to</param> /// <param name="config">App configuration</param> /// <param name="serviceName">cloud foundry service name binding</param> /// <param name="contextLifetime">Lifetime of the service to inject</param> /// <param name="logFactory">logger factory</param> /// <param name="healthChecksBuilder">Microsoft HealthChecksBuilder</param> /// <returns>IServiceCollection for chaining</returns> /// <remarks>RabbitMQ.Client.ConnectionFactory is retrievable as both ConnectionFactory and IConnectionFactory</remarks> public static IServiceCollection AddRabbitMQConnection(this IServiceCollection services, IConfiguration config, string serviceName, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ILoggerFactory logFactory = null, IHealthChecksBuilder healthChecksBuilder = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (string.IsNullOrEmpty(serviceName)) { throw new ArgumentNullException(nameof(serviceName)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } RabbitMQServiceInfo info = config.GetRequiredServiceInfo <RabbitMQServiceInfo>(serviceName); DoAdd(services, info, config, contextLifetime, healthChecksBuilder); return(services); }
public void Configure_SSLServiceInfoOveridesConfig_ReturnsOverriddenConnectionString() { RabbitMQProviderConnectorOptions config = new RabbitMQProviderConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", VirtualHost = "vhost" }; RabbitMQProviderConfigurer configurer = new RabbitMQProviderConfigurer(); RabbitMQServiceInfo si = new RabbitMQServiceInfo("MyId", "amqps://*****:*****@example.com/si_vhost"); var opts = configurer.Configure(si, config); var uri = new UriInfo(opts); Assert.Equal("example.com", uri.Host); Assert.Equal("amqps", uri.Scheme); Assert.Equal("si_username", uri.UserName); Assert.Equal("si_password", uri.Password); Assert.Equal("si_vhost", uri.Path); }
internal string Configure(RabbitMQServiceInfo si, RabbitMQProviderConnectorOptions configuration) { UpdateConfiguration(si, configuration); return(configuration.ToString()); }
public void Constructor_CreatesExpected() { var uri = "amqp://*****:*****@192.168.0.81:5672/fb03d693-91fe-4dc5-8203-ff7a6390df66"; var uris = new List <string>() { "amqp://*****:*****@192.168.0.81:5672/fb03d693-91fe-4dc5-8203-ff7a6390df66" }; var managementUri = "https://*****:*****@pivotal-rabbitmq.system.testcloud.com/api/"; var managementUris = new List <string>() { "https://*****:*****@pivotal-rabbitmq.system.testcloud.com/api/" }; var r1 = new RabbitMQServiceInfo("myId", uri); var r2 = new RabbitMQServiceInfo("myId", uri, managementUri); var r3 = new RabbitMQServiceInfo("myId", uri, managementUri, uris, managementUris); var r4 = new RabbitMQServiceInfo("myId", "192.168.0.81", 5672, "03c7a684-6ff1-4bd0-ad45-d10374ffb2af", "l5oq2q0unl35s6urfsuib0jvpo", "fb03d693-91fe-4dc5-8203-ff7a6390df66"); var r5 = new RabbitMQServiceInfo("myId", "192.168.0.81", 5672, "03c7a684-6ff1-4bd0-ad45-d10374ffb2af", "l5oq2q0unl35s6urfsuib0jvpo", "fb03d693-91fe-4dc5-8203-ff7a6390df66", "https://*****:*****@pivotal-rabbitmq.system.testcloud.com/api/"); Assert.Equal("myId", r1.Id); Assert.Equal("amqp", r1.Scheme); Assert.Equal("192.168.0.81", r1.Host); Assert.Equal(5672, r1.Port); Assert.Equal("l5oq2q0unl35s6urfsuib0jvpo", r1.Password); Assert.Equal("03c7a684-6ff1-4bd0-ad45-d10374ffb2af", r1.UserName); Assert.Equal("fb03d693-91fe-4dc5-8203-ff7a6390df66", r1.Path); Assert.Null(r1.Query); Assert.Null(r1.Uris); Assert.Equal(uri, r1.Uri); Assert.Null(r1.ManagementUris); Assert.Null(r1.ManagementUri); Assert.Equal("myId", r2.Id); Assert.Equal("amqp", r2.Scheme); Assert.Equal("192.168.0.81", r2.Host); Assert.Equal(5672, r2.Port); Assert.Equal("l5oq2q0unl35s6urfsuib0jvpo", r2.Password); Assert.Equal("03c7a684-6ff1-4bd0-ad45-d10374ffb2af", r2.UserName); Assert.Equal("fb03d693-91fe-4dc5-8203-ff7a6390df66", r2.Path); Assert.Null(r2.Query); Assert.Null(r2.Uris); Assert.Equal(uri, r2.Uri); Assert.Null(r2.ManagementUris); Assert.Equal(managementUri, r2.ManagementUri); Assert.Equal("myId", r3.Id); Assert.Equal("amqp", r3.Scheme); Assert.Equal("192.168.0.81", r3.Host); Assert.Equal(5672, r3.Port); Assert.Equal("l5oq2q0unl35s6urfsuib0jvpo", r3.Password); Assert.Equal("03c7a684-6ff1-4bd0-ad45-d10374ffb2af", r3.UserName); Assert.Equal("fb03d693-91fe-4dc5-8203-ff7a6390df66", r3.Path); Assert.Null(r3.Query); Assert.NotNull(r3.Uris); Assert.Single(r3.Uris); Assert.Equal(uris[0], r3.Uris[0]); Assert.Equal(uri, r3.Uri); Assert.NotNull(r3.ManagementUris); Assert.Single(r3.ManagementUris); Assert.Equal(managementUris[0], r3.ManagementUris[0]); Assert.Equal(managementUri, r3.ManagementUri); Assert.Equal("myId", r4.Id); Assert.Equal("amqp", r4.Scheme); Assert.Equal("192.168.0.81", r4.Host); Assert.Equal(5672, r4.Port); Assert.Equal("l5oq2q0unl35s6urfsuib0jvpo", r4.Password); Assert.Equal("03c7a684-6ff1-4bd0-ad45-d10374ffb2af", r4.UserName); Assert.Equal("fb03d693-91fe-4dc5-8203-ff7a6390df66", r4.Path); Assert.Null(r4.Query); Assert.Null(r4.Uris); Assert.Equal(uri, r4.Uri); Assert.Null(r4.ManagementUris); Assert.Null(r4.ManagementUri); Assert.Equal("myId", r5.Id); Assert.Equal("amqp", r5.Scheme); Assert.Equal("192.168.0.81", r5.Host); Assert.Equal(5672, r5.Port); Assert.Equal("l5oq2q0unl35s6urfsuib0jvpo", r5.Password); Assert.Equal("03c7a684-6ff1-4bd0-ad45-d10374ffb2af", r5.UserName); Assert.Equal("fb03d693-91fe-4dc5-8203-ff7a6390df66", r5.Path); Assert.Null(r5.Query); Assert.Null(r5.Uris); Assert.Equal(uri, r5.Uri); Assert.Null(r5.ManagementUris); Assert.Equal(managementUri, r5.ManagementUri); }