/// <summary> /// Adds MongoDb classes (MongoClient, IMongoClient and MongoUrl) 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> RegisterMongoDbConnection(this ContainerBuilder container, IConfiguration config, string serviceName = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } MongoDbServiceInfo info = serviceName == null ? config.GetSingletonServiceInfo <MongoDbServiceInfo>() : config.GetRequiredServiceInfo <MongoDbServiceInfo>(serviceName); var mongoOptions = new MongoDbConnectorOptions(config); var clientFactory = new MongoDbConnectorFactory(info, mongoOptions, MongoDbTypeLocator.MongoClient); var urlFactory = new MongoDbConnectorFactory(info, mongoOptions, MongoDbTypeLocator.MongoUrl); container.Register(c => urlFactory.Create(null)).As(MongoDbTypeLocator.MongoUrl); container.Register(c => new MongoDbHealthContributor(clientFactory, c.ResolveOptional <ILogger <MongoDbHealthContributor> >())).As <IHealthContributor>(); return(container.Register(c => clientFactory.Create(null)).As(MongoDbTypeLocator.IMongoClient, MongoDbTypeLocator.MongoClient)); }
private Connection GetConnection(MongoDbServiceInfo info, IConfiguration configuration) { var mongoConfig = new MongoDbConnectorOptions(configuration); var configurer = new MongoDbProviderConfigurer(); return(new Connection(configurer.Configure(info, mongoConfig), "MongoDb", info)); }
public void UpdateConfiguration(MongoDbServiceInfo si, MongoDbConnectorOptions configuration) { if (si == null) { return; } if (!string.IsNullOrEmpty(si.Uri)) { configuration.Uri = si.Uri; // the rest of this is unlikely to really be needed when a uri is available // but the properties are here, so let's go ahead and set them just in case 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.Database = si.Path; } }
public void Constructor_ThrowsIfConfigNull() { MongoDbConnectorOptions config = null; MongoDbServiceInfo si = null; var ex = Assert.Throws <ArgumentNullException>(() => new MongoDbConnectorFactory(si, config, MongoDbTypeLocator.MongoClient)); Assert.Contains(nameof(config), ex.Message); }
public void Is_Connected_Returns_Up_Status() { var mongoDbConfig = new MongoDbConnectorOptions(); var sInfo = new MongoDbServiceInfo("MyId", "mongodb://localhost:27017"); var logrFactory = new LoggerFactory(); var connFactory = new MongoDbConnectorFactory(sInfo, mongoDbConfig, mongoDbImplementationType); var h = new MongoDbHealthContributor(connFactory, logrFactory.CreateLogger <MongoDbHealthContributor>()); var status = h.Health(); Assert.Equal(HealthStatus.UP, status.Status); }
public void Not_Connected_Returns_Down_Status() { var mongoDbConfig = new MongoDbConnectorOptions(); var sInfo = new MongoDbServiceInfo("MyId", "mongodb://localhost:27018"); var logrFactory = new LoggerFactory(); var connFactory = new MongoDbConnectorFactory(sInfo, mongoDbConfig, mongoDbImplementationType); var h = new MongoDbHealthContributor(connFactory, logrFactory.CreateLogger <MongoDbHealthContributor>(), 1); var status = h.Health(); Assert.Equal(HealthStatus.DOWN, status.Status); Assert.Equal("Failed to open MongoDb connection!", status.Description); }
private static void DoAdd(IServiceCollection services, MongoDbServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime) { Type mongoClient = MongoDbTypeLocator.MongoClient; var mongoOptions = new MongoDbConnectorOptions(config); var clientFactory = new MongoDbConnectorFactory(info, mongoOptions, mongoClient); services.Add(new ServiceDescriptor(MongoDbTypeLocator.IMongoClient, clientFactory.Create, contextLifetime)); services.Add(new ServiceDescriptor(mongoClient, clientFactory.Create, contextLifetime)); services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new MongoDbHealthContributor(clientFactory, ctx.GetService <ILogger <MongoDbHealthContributor> >()), ServiceLifetime.Singleton)); Type mongoInfo = ConnectorHelpers.FindType(MongoDbTypeLocator.Assemblies, MongoDbTypeLocator.MongoConnectionInfo); var urlFactory = new MongoDbConnectorFactory(info, mongoOptions, mongoInfo); services.Add(new ServiceDescriptor(mongoInfo, urlFactory.Create, contextLifetime)); }
public void Create_ReturnsMongoDbConnection() { var config = new MongoDbConnectorOptions() { Server = "localhost", Port = 27016, Password = "******", Username = "******", }; var si = new MongoDbServiceInfo("MyId", "mongodb://localhost:27017"); var factory = new MongoDbConnectorFactory(si, config, MongoDbTypeLocator.MongoClient); var connection = factory.Create(null); Assert.NotNull(connection); }
public void Constructor_HandlesReplicas() { var uri = "mongodb://*****:*****@mongodb-0.node.dc1.a9s-mongodb-consul:27017,mongodb-1.node.dc1.a9s-mongodb-consul:27017,mongodb-2.node.dc1.a9s-mongodb-consul:27017/d5584e9?replicaSet=rs0"; var r1 = new MongoDbServiceInfo("myId", uri); Assert.Equal("myId", r1.Id); Assert.Equal("mongodb", r1.Scheme); Assert.Contains("mongodb-0.node.dc1.a9s-mongodb-consul:27017", r1.Hosts); Assert.Contains("mongodb-1.node.dc1.a9s-mongodb-consul:27017", r1.Hosts); Assert.Contains("mongodb-2.node.dc1.a9s-mongodb-consul:27017", r1.Hosts); Assert.Equal("a9sb8b69cc", r1.Password); Assert.Equal("a9s-brk-usr-e74b9538", r1.UserName); Assert.Equal("d5584e9", r1.Path); Assert.Equal("replicaSet=rs0", r1.Query); }
private static void DoAdd(IServiceCollection services, MongoDbServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime) { Type mongoClient = MongoDbTypeLocator.MongoClient; var mongoOptions = new MongoDbConnectorOptions(config); var clientFactory = new MongoDbConnectorFactory(info, mongoOptions, mongoClient); services.Add(new ServiceDescriptor(MongoDbTypeLocator.IMongoClient, clientFactory.Create, contextLifetime)); services.Add(new ServiceDescriptor(mongoClient, clientFactory.Create, contextLifetime)); Type mongoInfo = ConnectorHelpers.FindType(MongoDbTypeLocator.Assemblies, MongoDbTypeLocator.MongoConnectionInfo); var urlFactory = new MongoDbConnectorFactory(info, mongoOptions, mongoInfo); services.Add(new ServiceDescriptor(mongoInfo, urlFactory.Create, contextLifetime)); // TODO: add health check }
/// <summary> /// Add MongoDb 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> /// <returns>IServiceCollection for chaining</returns> public static IServiceCollection AddMongoClient(this IServiceCollection services, IConfiguration config, ServiceLifetime contextLifetime = ServiceLifetime.Singleton) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } MongoDbServiceInfo info = config.GetSingletonServiceInfo <MongoDbServiceInfo>(); DoAdd(services, info, config, contextLifetime); return(services); }
private static void DoAdd(IServiceCollection services, MongoDbServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, bool addSteeltoeHealthChecks = false) { var mongoClient = MongoDbTypeLocator.MongoClient; var mongoOptions = new MongoDbConnectorOptions(config); var clientFactory = new MongoDbConnectorFactory(info, mongoOptions, mongoClient); services.Add(new ServiceDescriptor(MongoDbTypeLocator.IMongoClient, clientFactory.Create, contextLifetime)); services.Add(new ServiceDescriptor(mongoClient, clientFactory.Create, contextLifetime)); if (!services.Any(s => s.ServiceType == typeof(HealthCheckService)) || addSteeltoeHealthChecks) { services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new MongoDbHealthContributor(clientFactory, ctx.GetService <ILogger <MongoDbHealthContributor> >()), ServiceLifetime.Singleton)); } var mongoInfo = ReflectionHelpers.FindType(MongoDbTypeLocator.Assemblies, MongoDbTypeLocator.MongoConnectionInfo); var urlFactory = new MongoDbConnectorFactory(info, mongoOptions, mongoInfo); services.Add(new ServiceDescriptor(mongoInfo, urlFactory.Create, contextLifetime)); }
public void UpdateConfiguration_WithMongoDbServiceInfo_ReturnsExpected() { MongoDbProviderConfigurer configurer = new MongoDbProviderConfigurer(); MongoDbConnectorOptions config = new MongoDbConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", Database = "database" }; MongoDbServiceInfo si = new MongoDbServiceInfo("MyId", "mongodb://*****:*****@192.168.0.90:27017/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355"); configurer.UpdateConfiguration(si, config); Assert.Equal("192.168.0.90", config.Server); Assert.Equal(27017, config.Port); Assert.Equal("Dd6O1BPXUHdrmzbP", config.Username); Assert.Equal("7E1LxXnlH2hhlPVt", config.Password); Assert.Equal("cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355", config.Database); }
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); }
/// <summary> /// Add MongoDb 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="addSteeltoeHealthChecks">Add Steeltoe healthChecks</param> /// <returns>IServiceCollection for chaining</returns> public static IServiceCollection AddMongoClient(this IServiceCollection services, IConfiguration config, string serviceName, ServiceLifetime contextLifetime = ServiceLifetime.Singleton, bool addSteeltoeHealthChecks = false) { 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)); } MongoDbServiceInfo info = config.GetRequiredServiceInfo <MongoDbServiceInfo>(serviceName); DoAdd(services, info, config, contextLifetime, addSteeltoeHealthChecks); return(services); }
public void Configure_ServiceInfoOveridesConfig_ReturnsExpected() { MongoDbConnectorOptions config = new MongoDbConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", Database = "database" }; MongoDbProviderConfigurer configurer = new MongoDbProviderConfigurer(); MongoDbServiceInfo si = new MongoDbServiceInfo("MyId", "mongodb://*****:*****@192.168.0.90:27017/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355"); var opts = configurer.Configure(si, config); Assert.Equal("mongodb://*****:*****@192.168.0.90:27017/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355", opts); Assert.DoesNotContain("localhost", opts); Assert.DoesNotContain("1234", opts); Assert.DoesNotContain("username", opts); Assert.DoesNotContain("password", opts); Assert.DoesNotContain("database", opts); }
public void Constructor_CreatesExpected() { var uri = "mongodb://*****:*****@192.168.0.90:27017/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355"; var r1 = new MongoDbServiceInfo("myId", uri); var r2 = new MongoDbServiceInfo("myId", "192.168.0.90", 27017, "Dd6O1BPXUHdrmzbP", "7E1LxXnlH2hhlPVt", "cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355"); Assert.Equal("myId", r1.Id); Assert.Equal("mongodb", r1.Scheme); Assert.Equal("192.168.0.90", r1.Host); Assert.Equal(27017, r1.Port); Assert.Equal("7E1LxXnlH2hhlPVt", r1.Password); Assert.Equal("Dd6O1BPXUHdrmzbP", r1.UserName); Assert.Equal("cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355", r1.Path); Assert.Null(r1.Query); Assert.Equal("myId", r2.Id); Assert.Equal("mongodb", r2.Scheme); Assert.Equal("192.168.0.90", r2.Host); Assert.Equal(27017, r2.Port); Assert.Equal("7E1LxXnlH2hhlPVt", r2.Password); Assert.Equal("Dd6O1BPXUHdrmzbP", r2.UserName); Assert.Equal("cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355", r2.Path); Assert.Null(r2.Query); }
public string Configure(MongoDbServiceInfo si, MongoDbConnectorOptions configuration) { UpdateConfiguration(si, configuration); return(configuration.ToString()); }
public MongoDbConnectorFactory(MongoDbServiceInfo sinfo, MongoDbConnectorOptions config, Type type) { _info = sinfo; _config = config ?? throw new ArgumentNullException(nameof(config)); ConnectorType = type; }