public void Constructor_BindsValues() { var appsettings = new Dictionary <string, string>() { ["mysql:client:server"] = "localhost", ["mysql:client:port"] = "1234", ["mysql:client:PersistSecurityInfo"] = "true", ["mysql:client:password"] = "******", ["mysql:client:username"] = "******" }; var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); var config = configurationBuilder.Build(); var sconfig = new MySqlProviderConnectorOptions(config); Assert.Equal("localhost", sconfig.Server); Assert.Equal(1234, sconfig.Port); Assert.True(sconfig.PersistSecurityInfo); Assert.Equal("password", sconfig.Password); Assert.Equal("username", sconfig.Username); Assert.Null(sconfig.ConnectionString); }
public void ConnectionString_Overridden_By_CloudFoundryConfig() { // arrange // simulate an appsettings file var appsettings = new Dictionary <string, string>() { ["mysql:client:ConnectionString"] = "Server=fake;Database=test;Uid=steeltoe;Pwd=password;" }; // add environment variables as Cloud Foundry would Environment.SetEnvironmentVariable("VCAP_APPLICATION", TestHelpers.VCAP_APPLICATION); Environment.SetEnvironmentVariable("VCAP_SERVICES", MySqlTestHelpers.SingleServerVCAP); // add settings to config var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); configurationBuilder.AddEnvironmentVariables(); configurationBuilder.AddCloudFoundry(); var config = configurationBuilder.Build(); // act var sconfig = new MySqlProviderConnectorOptions(config); // assert Assert.NotEqual(appsettings["mysql:client:ConnectionString"], sconfig.ToString()); // NOTE: for this test, we don't expect VCAP_SERVICES to be parsed, // this test is only here to demonstrate that when a binding is present, // a pre-supplied connectionString is not returned }
/// <summary> /// Adds a MySqlConnection (as IDbConnection) 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> RegisterMySqlConnection(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 mySqlConnection = ConnectorHelpers.FindType(mySqlAssemblies, mySqlTypeNames); if (mySqlConnection == null) { throw new ConnectorException("Unable to find MySqlConnection, are you missing a MySql reference?"); } MySqlServiceInfo info; if (serviceName == null) { info = config.GetSingletonServiceInfo <MySqlServiceInfo>(); } else { info = config.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName); } MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config); MySqlProviderConnectorFactory factory = new MySqlProviderConnectorFactory(info, mySqlConfig, mySqlConnection); return(container.Register(c => factory.Create(null)).As <IDbConnection>()); }
public void Constructor_BindsValues() { var appsettings = @" { 'mysql': { 'client': { 'server': 'localhost', 'port': 1234, 'password': '******', 'username': '******' } } }"; var path = TestHelpers.CreateTempFile(appsettings); string directory = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(directory); configurationBuilder.AddJsonFile(fileName); var config = configurationBuilder.Build(); var sconfig = new MySqlProviderConnectorOptions(config); Assert.Equal("localhost", sconfig.Server); Assert.Equal(1234, sconfig.Port); Assert.Equal("password", sconfig.Password); Assert.Equal("username", sconfig.Username); Assert.Equal(null, sconfig.ConnectionString); }
public void ConnectionString_Overridden_By_CloudFoundryConfig() { // arrange // simulate an appsettings file var appsettings = new Dictionary <string, string>() { ["mysql:client:ConnectionString"] = "Server=fake;Database=test;Uid=steeltoe;Pwd=password;" }; // add environment variables as Cloud Foundry would Environment.SetEnvironmentVariable("VCAP_APPLICATION", TestHelpers.VCAP_APPLICATION); Environment.SetEnvironmentVariable("VCAP_SERVICES", MySqlTestHelpers.SingleServerVCAP); // add settings to config ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); configurationBuilder.AddEnvironmentVariables(); configurationBuilder.AddCloudFoundry(); var config = configurationBuilder.Build(); // act var sconfig = new MySqlProviderConnectorOptions(config); // assert Assert.NotEqual(appsettings["mysql:client:ConnectionString"], sconfig.ToString()); }
private static void DoAdd(IServiceCollection services, IConfiguration config, MySqlServiceInfo info, Type dbContextType, ServiceLifetime contextLifetime) { MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config); MySqlDbContextConnectorFactory factory = new MySqlDbContextConnectorFactory(info, mySqlConfig, dbContextType); services.Add(new ServiceDescriptor(dbContextType, factory.Create, contextLifetime)); }
public static void RegisterMySqlConnection(this ContainerBuilder container, IConfigurationRoot config) { MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config); MySqlServiceInfo info = config.GetSingletonServiceInfo <MySqlServiceInfo>(); MySqlProviderConnectorFactory factory = new MySqlProviderConnectorFactory(info, mySqlConfig); container.Register <MySqlConnection>(c => (MySqlConnection)factory.Create(null)).InstancePerLifetimeScope(); }
public MySqlDbContextConnectorFactory(MySqlServiceInfo info, MySqlProviderConnectorOptions config, Type dbContextType) : base(info, config, dbContextType) { if (dbContextType == null) { throw new ArgumentNullException(nameof(dbContextType)); } }
public void Constructor_ThrowsIfConfigNull() { MySqlProviderConnectorOptions config = null; MySqlServiceInfo si = null; var ex = Assert.Throws <ArgumentNullException>(() => new MySqlProviderConnectorFactory(si, config, typeof(MySqlConnection))); Assert.Contains(nameof(config), ex.Message); }
/// <summary> /// Mysql连接字符串构建 /// </summary> /// <param name="config"></param> /// <param name="serviceName"></param> /// <returns></returns> public static string BuildeMysqlConnectionString(this IConfiguration config, string serviceName = null) { MySqlServiceInfo info = string.IsNullOrEmpty(serviceName) ? config.GetSingletonServiceInfo <MySqlServiceInfo>() : config.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName); MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config); MySqlProviderConnectorFactory factory = new MySqlProviderConnectorFactory(info, mySqlConfig, null); return(factory.CreateConnectionString()); }
public void Create_ThrowsIfNoValidConstructorFound() { var config = new MySqlProviderConnectorOptions(); MySqlServiceInfo si = null; var dbContextType = typeof(BadMySqlDbContext); var ex = Assert.Throws <ConnectorException>(() => new MySqlDbContextConnectorFactory(si, config, dbContextType).Create(null)); Assert.Contains("BadMySqlDbContext", ex.Message); }
public void Constructor_ThrowsIfTypeNull() { var config = new MySqlProviderConnectorOptions(); MySqlServiceInfo si = null; Type dbContextType = null; var ex = Assert.Throws <ArgumentNullException>(() => new MySqlDbContextConnectorFactory(si, config, dbContextType)); Assert.Contains(nameof(dbContextType), ex.Message); }
private static void DoAdd(IServiceCollection services, IConfiguration config, MySqlServiceInfo info, Type dbContextType, ServiceLifetime contextLifetime) { var mySqlConfig = new MySqlProviderConnectorOptions(config); var factory = new MySqlDbContextConnectorFactory(info, mySqlConfig, dbContextType); services.Add(new ServiceDescriptor(dbContextType, factory.Create, contextLifetime)); var healthFactory = new MySqlProviderConnectorFactory(info, mySqlConfig, MySqlTypeLocator.MySqlConnection); services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RelationalDbHealthContributor((IDbConnection)healthFactory.Create(ctx), ctx.GetService <ILogger <RelationalDbHealthContributor> >()), contextLifetime)); }
public void Constructor_ThrowsIfNoValidConstructorFound() { // Arrange MySqlProviderConnectorOptions config = new MySqlProviderConnectorOptions(); MySqlServiceInfo si = null; Type dbContextType = typeof(BadDbContext); // Act and Assert var ex = Assert.Throws <ConnectorException>(() => new MySqlDbContextConnectorFactory(si, config, dbContextType)); Assert.Contains("BadDbContext", ex.Message); }
private static string GetConnection(IConfiguration config, string serviceName = null) { var info = string.IsNullOrEmpty(serviceName) ? config.GetSingletonServiceInfo <MySqlServiceInfo>() : config.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName); var mySqlConfig = new MySqlProviderConnectorOptions(config); var factory = new MySqlProviderConnectorFactory(info, mySqlConfig, null); return(factory.CreateConnectionString()); }
private IEnumerable <IHealthContributor> GetHealthContributors() { var info = ApplicationConfig.Configuration.GetSingletonServiceInfo <MySqlServiceInfo>(); var mySqlConfig = new MySqlProviderConnectorOptions(ApplicationConfig.Configuration); var factory = new MySqlProviderConnectorFactory(info, mySqlConfig, MySqlTypeLocator.MySqlConnection); var healthContributors = new List <IHealthContributor> { new DiskSpaceContributor(), new RelationalHealthContributor(new MySqlConnection(factory.CreateConnectionString())) }; return(healthContributors); }
public static IHealthContributor GetMySqlContributor(IConfiguration configuration, ILogger <SkynetCloudRelationalHealthContributor> logger = null) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var info = configuration.GetSingletonServiceInfo <MySqlServiceInfo>(); Type mySqlConnection = ConnectorHelpers.FindType(MySqlTypeLocator.Assemblies, MySqlTypeLocator.ConnectionTypeNames); var mySqlConfig = new MySqlProviderConnectorOptions(configuration); var factory = new MySqlProviderConnectorFactory(info, mySqlConfig, mySqlConnection); var connection = factory.Create(null) as IDbConnection; return(new SkynetCloudRelationalHealthContributor(connection, logger)); }
public void ConnectionString_Returned_AsConfigured() { var appsettings = new Dictionary <string, string>() { ["mysql:client:ConnectionString"] = "Server=fake;Database=test;Uid=steeltoe;Pwd=password;" }; var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); var config = configurationBuilder.Build(); var sconfig = new MySqlProviderConnectorOptions(config); Assert.Equal(appsettings["mysql:client:ConnectionString"], sconfig.ToString()); }
public MySqlDbContextConnectorFactory(MySqlServiceInfo info, MySqlProviderConnectorOptions config, Type dbContextType) : base(info, config) { if (dbContextType == null) { throw new ArgumentNullException(nameof(dbContextType)); } this.dbContextType = dbContextType; this.constructor = FindConstructor(dbContextType); if (this.constructor == null) { throw new ConnectorException(string.Format("Missing 'public {0}(string connectionString)' constructor", dbContextType)); } }
public Connection Get(IConfiguration configuration, string serviceName) { var info = serviceName == null ? configuration.GetSingletonServiceInfo <MySqlServiceInfo>() : configuration.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName); var mySqlConfig = new MySqlProviderConnectorOptions(configuration); var configurer = new MySqlProviderConfigurer(); var connString = configurer.Configure(info, mySqlConfig); return(new Connection { ConnectionString = connString, Name = "MySql" + serviceName?.Insert(0, "-") }); }
public void Create_ReturnsMySqlConnection() { MySqlProviderConnectorOptions config = new MySqlProviderConnectorOptions() { Server = "localhost", Port = 3306, Password = "******", Username = "******", Database = "database" }; MySqlServiceInfo si = new MySqlServiceInfo("MyId", "mysql://*****:*****@192.168.0.90:3306/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355"); var factory = new MySqlProviderConnectorFactory(si, config, typeof(MySqlConnection)); var connection = factory.Create(null); Assert.NotNull(connection); }
public void MySql_Is_Connected_Returns_Up_Status() { var implementationType = MySqlTypeLocator.MySqlConnection; var sqlConfig = new MySqlProviderConnectorOptions() { ConnectionTimeout = 1 }; var sInfo = new MySqlServiceInfo("MyId", "mysql://*****:*****@localhost:3306"); var logrFactory = new LoggerFactory(); var connFactory = new MySqlProviderConnectorFactory(sInfo, sqlConfig, implementationType); var h = new RelationalDbHealthContributor((IDbConnection)connFactory.Create(null), logrFactory.CreateLogger <RelationalDbHealthContributor>()); var status = h.Health(); Assert.Equal(HealthStatus.UP, status.Status); }
public void MySql_Not_Connected_Returns_Down_Status() { var implementationType = MySqlTypeLocator.MySqlConnection; var sqlConfig = new MySqlProviderConnectorOptions() { ConnectionTimeout = 1 }; var sInfo = new MySqlServiceInfo("MyId", "mysql://localhost:80;databaseName=invalidDatabaseName"); var logrFactory = new LoggerFactory(); var connFactory = new MySqlProviderConnectorFactory(sInfo, sqlConfig, implementationType); var h = new RelationalDbHealthContributor((IDbConnection)connFactory.Create(null), logrFactory.CreateLogger <RelationalDbHealthContributor>()); var status = h.Health(); Assert.Equal(HealthStatus.DOWN, status.Status); Assert.Contains(status.Details.Keys, k => k == "error"); }
private static string GetConnection(IConfiguration config, string serviceName = null) { MySqlServiceInfo info = null; if (string.IsNullOrEmpty(serviceName)) { info = config.GetSingletonServiceInfo <MySqlServiceInfo>(); } else { info = config.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName); } MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config); MySqlProviderConnectorFactory factory = new MySqlProviderConnectorFactory(info, mySqlConfig); return(factory.CreateConnectionString()); }
public void Configure_NoServiceInfo_ReturnsExpected() { MySqlProviderConnectorOptions config = new MySqlProviderConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", Database = "database" }; MySqlProviderConfigurer configurer = new MySqlProviderConfigurer(); var opts = configurer.Configure(null, config); Assert.Contains("Server=localhost;", opts); Assert.Contains("Port=1234;", opts); Assert.Contains("Username=username;", opts); Assert.Contains("Password=password;", opts); Assert.Contains("Database=database;", opts); }
public void UpdateConfiguration_WithMySqlServiceInfo_ReturnsExpected() { MySqlProviderConfigurer configurer = new MySqlProviderConfigurer(); MySqlProviderConnectorOptions config = new MySqlProviderConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", Database = "database" }; MySqlServiceInfo si = new MySqlServiceInfo("MyId", "mysql://*****:*****@192.168.0.90:3306/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355"); configurer.UpdateConfiguration(si, config); Assert.Equal("192.168.0.90", config.Server); Assert.Equal(3306, config.Port); Assert.Equal("Dd6O1BPXUHdrmzbP", config.Username); Assert.Equal("7E1LxXnlH2hhlPVt", config.Password); Assert.Equal("cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355", config.Database); }
public void UpdateConfiguration_WithNullMySqlServiceInfo_ReturnsExpected() { MySqlProviderConfigurer configurer = new MySqlProviderConfigurer(); MySqlProviderConnectorOptions config = new MySqlProviderConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", Database = "database" }; configurer.UpdateConfiguration(null, config); Assert.Equal("localhost", config.Server); Assert.Equal(1234, config.Port); Assert.Equal("username", config.Username); Assert.Equal("password", config.Password); Assert.Equal("database", config.Database); Assert.Null(config.ConnectionString); }
/// <summary> /// Add your MySql-based DbContext to the ContainerBuilder /// </summary> /// <typeparam name="TContext">Your DbContext</typeparam> /// <param name="container">Autofac <see cref="ContainerBuilder" /></param> /// <param name="config">Your app config</param> /// <param name="serviceName">Name of service instance</param> /// <returns><see cref="IRegistrationBuilder{TLimit, TActivatorData, TRegistrationStyle}"/></returns> public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterDbContext <TContext>(this ContainerBuilder container, IConfiguration config, string serviceName = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } MySqlServiceInfo info = serviceName == null ? config.GetSingletonServiceInfo <MySqlServiceInfo>() : config.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName); var mySqlConfig = new MySqlProviderConnectorOptions(config); var factory = new MySqlProviderConnectorFactory(info, mySqlConfig, typeof(TContext)); return(container.Register(c => factory.Create(null)).As <TContext>()); }
public void Configure_ServiceInfoOveridesConfig_ReturnsExpected() { MySqlProviderConnectorOptions config = new MySqlProviderConnectorOptions() { Server = "localhost", Port = 1234, Username = "******", Password = "******", Database = "database" }; MySqlProviderConfigurer configurer = new MySqlProviderConfigurer(); MySqlServiceInfo si = new MySqlServiceInfo("MyId", "mysql://*****:*****@192.168.0.90:3306/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355"); var opts = configurer.Configure(si, config); Assert.Contains("Server=192.168.0.90;", opts); Assert.Contains("Port=3306;", opts); Assert.Contains("Username=Dd6O1BPXUHdrmzbP;", opts); Assert.Contains("Password=7E1LxXnlH2hhlPVt;", opts); Assert.Contains("Database=cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355;", opts); }
/// <summary> /// Adds a MySqlConnection (as IDbConnection and MySqlConnection) 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> RegisterMySqlConnection(this ContainerBuilder container, IConfiguration config, string serviceName = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } MySqlServiceInfo info = serviceName == null ? config.GetSingletonServiceInfo <MySqlServiceInfo>() : config.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName); Type mySqlConnection = MySqlTypeLocator.MySqlConnection; var mySqlConfig = new MySqlProviderConnectorOptions(config); var factory = new MySqlProviderConnectorFactory(info, mySqlConfig, mySqlConnection); container.RegisterType <RelationalHealthContributor>().As <IHealthContributor>(); return(container.Register(c => factory.Create(null)).As(typeof(IDbConnection), mySqlConnection)); }