예제 #1
0
        public void UpdateConfiguration(MySqlServiceInfo si, MySqlProviderConnectorOptions configuration)
        {
            if (si == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(si.Uri))
            {
                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;
            }
        }
예제 #2
0
        private Connection GetConnection(MySqlServiceInfo info, IConfiguration configuration)
        {
            var mySqlConfig = new MySqlProviderConnectorOptions(configuration);
            var configurer  = new MySqlProviderConfigurer();

            return(new Connection(configurer.Configure(info, mySqlConfig), "MySql", info));
        }
        private static void DoAdd(IServiceCollection services, MySqlServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config);
            MySqlProviderConnectorFactory factory     = new MySqlProviderConnectorFactory(info, mySqlConfig);

            services.Add(new ServiceDescriptor(typeof(MySqlConnection), factory.Create, contextLifetime));
        }
예제 #4
0
        private static void DoAdd(IServiceCollection services, MySqlServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            var mySqlConfig = new MySqlProviderConnectorOptions(config);
            var factory     = new MySqlProviderConnectorFactory(info, mySqlConfig, MySqlTypeLocator.MySqlConnection);

            services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RelationalHealthContributor((IDbConnection)factory.Create(ctx), ctx.GetService <ILogger <RelationalHealthContributor> >()), contextLifetime));
        }
예제 #5
0
 public MySqlDbContextConnectorFactory(MySqlServiceInfo info, MySqlProviderConnectorOptions config, Type dbContextType)
     : base(info, config, dbContextType)
 {
     if (dbContextType == null)
     {
         throw new ArgumentNullException(nameof(dbContextType));
     }
 }
예제 #6
0
        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();
        }
예제 #7
0
        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);
        }
예제 #8
0
        private static void DoAdd(IServiceCollection services, MySqlServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            Type mySqlConnection = ConnectorHelpers.FindType(MySqlTypeLocator.Assemblies, MySqlTypeLocator.ConnectionTypeNames);
            var  mySqlConfig     = new MySqlProviderConnectorOptions(config);
            var  factory         = new MySqlProviderConnectorFactory(info, mySqlConfig, mySqlConnection);

            services.Add(new ServiceDescriptor(typeof(IDbConnection), factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(mySqlConnection, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RelationalHealthContributor((IDbConnection)factory.Create(ctx), ctx.GetService <ILogger <RelationalHealthContributor> >()), ServiceLifetime.Singleton));
        }
        /// <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());
        }
예제 #10
0
        public MySqlProviderConnectorFactory(MySqlServiceInfo sinfo, MySqlProviderConnectorOptions config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            _info   = sinfo;
            _config = config;
        }
        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);
        }
예제 #13
0
        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);
        }
예제 #14
0
        public void Constructor_CreatesExpected()
        {
            string           uri = "mysql://*****:*****@192.168.0.90:3306/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355?reconnect=true";
            MySqlServiceInfo r1  = new MySqlServiceInfo("myId", uri);

            Assert.Equal("myId", r1.Id);
            Assert.Equal("mysql", r1.Scheme);
            Assert.Equal("192.168.0.90", r1.Host);
            Assert.Equal(3306, r1.Port);
            Assert.Equal("7E1LxXnlH2hhlPVt", r1.Password);
            Assert.Equal("Dd6O1BPXUHdrmzbP", r1.UserName);
            Assert.Equal("cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355", r1.Path);
            Assert.Equal("reconnect=true", r1.Query);
        }
예제 #15
0
        private static void DoAdd(IServiceCollection services, MySqlServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, bool addSteeltoeHealthChecks)
        {
            var mySqlConnection = ReflectionHelpers.FindType(MySqlTypeLocator.Assemblies, MySqlTypeLocator.ConnectionTypeNames);
            var mySqlConfig     = new MySqlProviderConnectorOptions(config);
            var factory         = new MySqlProviderConnectorFactory(info, mySqlConfig, mySqlConnection);

            services.Add(new ServiceDescriptor(typeof(IDbConnection), factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(mySqlConnection, factory.Create, contextLifetime));

            if (!services.Any(s => s.ServiceType == typeof(HealthCheckService)) || addSteeltoeHealthChecks)
            {
                services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RelationalDbHealthContributor((IDbConnection)factory.Create(ctx), ctx.GetService <ILogger <RelationalDbHealthContributor> >()), ServiceLifetime.Singleton));
            }
        }
예제 #16
0
        private static void DoAdd(IServiceCollection services, MySqlServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            Type mySqlConnection = ConnectorHelpers.FindType(mySqlAssemblies, mySqlTypeNames);

            if (mySqlConnection == null)
            {
                throw new ConnectorException("Unable to find MySqlConnection, are you missing MySql ADO.NET assembly");
            }

            MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config);
            MySqlProviderConnectorFactory factory     = new MySqlProviderConnectorFactory(info, mySqlConfig, mySqlConnection);

            services.Add(new ServiceDescriptor(typeof(IDbConnection), factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(mySqlConnection, factory.Create, contextLifetime));
        }
        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 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);
        }
예제 #19
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 UpdateConfiguration(MySqlServiceInfo si, MySqlProviderConnectorOptions configuration)
        {
            if (si == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(si.Uri))
            {
                configuration.Port     = si.Port;
                configuration.Username = si.UserName;
                configuration.Password = si.Password;
                configuration.Server   = si.Host;
                configuration.Database = si.Path;
            }
        }
        /// <summary>
        /// Add MySql and its IHealthContributor 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">logging factory</param>
        /// <param name="builder">Microsoft HealthChecksBuilder</param>
        /// <returns>IServiceCollection for chaining</returns>
        /// <remarks>MySqlConnection is retrievable as both MySqlConnection and IDbConnection</remarks>
        public static IServiceCollection AddMySqlConnection(this IServiceCollection services, IConfiguration config, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ILoggerFactory logFactory = null, IHealthChecksBuilder builder = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            MySqlServiceInfo info = config.GetSingletonServiceInfo <MySqlServiceInfo>();

            DoAdd(services, info, config, contextLifetime, builder);
            return(services);
        }
        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");
        }
        public static IServiceCollection AddDbContext <TContext>(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));
            }

            MySqlServiceInfo info = config.GetSingletonServiceInfo <MySqlServiceInfo>();

            DoAdd(services, config, info, typeof(TContext), contextLifetime);

            return(services);
        }
        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 static IServiceCollection AddMySqlConnection(this IServiceCollection services, IConfiguration config, ILoggerFactory logFactory = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }


            MySqlProviderConfiguration    mySqlConfig = new MySqlProviderConfiguration(config);
            MySqlServiceInfo              info        = config.GetSingletonServiceInfo <MySqlServiceInfo>();
            MySqlProviderConnectorFactory factory     = new MySqlProviderConnectorFactory(info, mySqlConfig);

            services.AddScoped(typeof(MySqlConnection), factory.Create);
            return(services);
        }
        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 static IServiceCollection AddMySqlConnection(this IServiceCollection services, IConfiguration config, string serviceName, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ILoggerFactory logFactory = 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));
            }
            MySqlServiceInfo info = config.GetRequiredServiceInfo <MySqlServiceInfo>(serviceName);

            DoAdd(services, info, config, contextLifetime);
            return(services);
        }
예제 #28
0
        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);
        }
예제 #29
0
        /// <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>());
        }
예제 #30
0
        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);
        }