Exemplo n.º 1
0
        /// <summary>
        /// Adds NpgsqlConnection (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> RegisterPostgreSqlConnection(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 postgreSqlConnection = ConnectorHelpers.FindType(postgreSqlAssemblies, postgreSqlTypeNames);

            if (postgreSqlConnection == null)
            {
                throw new ConnectorException("Unable to find NpgsqlConnection, are you missing a reference to Npgsql?");
            }

            PostgresServiceInfo info;

            if (serviceName == null)
            {
                info = config.GetSingletonServiceInfo <PostgresServiceInfo>();
            }
            else
            {
                info = config.GetRequiredServiceInfo <PostgresServiceInfo>(serviceName);
            }

            var postgreSqlConfig = new PostgresProviderConnectorOptions(config);
            PostgresProviderConnectorFactory factory = new PostgresProviderConnectorFactory(info, postgreSqlConfig, postgreSqlConnection);

            return(container.Register(c => factory.Create(null)).As <IDbConnection>());
        }
        /// <summary>
        /// Postgre连接字符串构建
        /// </summary>
        /// <param name="config"></param>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public static string BuildePostgreConnectionString(this IConfiguration config, string serviceName = null)
        {
            PostgresServiceInfo info = string.IsNullOrEmpty(serviceName)
                    ? config.GetSingletonServiceInfo <PostgresServiceInfo>()
                    : config.GetRequiredServiceInfo <PostgresServiceInfo>(serviceName);

            PostgresProviderConnectorOptions mySqlConfig = new PostgresProviderConnectorOptions(config);
            PostgresProviderConnectorFactory factory     = new PostgresProviderConnectorFactory(info, mySqlConfig, null);

            return(factory.CreateConnectionString());
        }
        private static string GetConnection(IConfiguration config, string serviceName = null)
        {
            var info = string.IsNullOrEmpty(serviceName)
                ? config.GetSingletonServiceInfo <PostgresServiceInfo>()
                : config.GetRequiredServiceInfo <PostgresServiceInfo>(serviceName);

            var postgresConfig = new PostgresProviderConnectorOptions(config);

            var factory = new PostgresProviderConnectorFactory(info, postgresConfig, null);

            return(factory.CreateConnectionString());
        }
        public void PostgreSql_Is_Connected_Returns_Up_Status()
        {
            var implementationType = PostgreSqlTypeLocator.NpgsqlConnection;
            var sqlConfig          = new PostgresProviderConnectorOptions();
            var sInfo       = new PostgresServiceInfo("MyId", "postgres://*****:*****@localhost:5432/postgres");
            var logrFactory = new LoggerFactory();
            var connFactory = new PostgresProviderConnectorFactory(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 static IHealthContributor GetPostgreSqlContributor(IConfiguration configuration, ILogger <SkynetCloudRelationalHealthContributor> logger = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var  info = configuration.GetSingletonServiceInfo <PostgresServiceInfo>();
            Type postgresConnection = ConnectorHelpers.FindType(PostgreSqlTypeLocator.Assemblies, PostgreSqlTypeLocator.ConnectionTypeNames);
            var  postgresConfig     = new PostgresProviderConnectorOptions(configuration);
            var  factory            = new PostgresProviderConnectorFactory(info, postgresConfig, postgresConnection);
            var  connection         = factory.Create(null) as IDbConnection;

            return(new SkynetCloudRelationalHealthContributor(connection, logger));
        }
Exemplo n.º 6
0
        public void Create_ReturnsPostgresConnection()
        {
            var config = new PostgresProviderConnectorOptions()
            {
                Host     = "localhost",
                Port     = 3306,
                Password = "******",
                Username = "******",
                Database = "database"
            };
            var si         = new PostgresServiceInfo("MyId", "postgres://*****:*****@192.168.0.90:5432/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355");
            var factory    = new PostgresProviderConnectorFactory(si, config, typeof(NpgsqlConnection));
            var connection = factory.Create(null);

            Assert.NotNull(connection);
        }
Exemplo n.º 7
0
        public void PostgreSql_Not_Connected_Returns_Down_Status()
        {
            // arrange
            Type implementationType = PostgreSqlTypeLocator.NpgsqlConnection;
            var  sqlConfig          = new PostgresProviderConnectorOptions();
            var  sInfo       = new PostgresServiceInfo("MyId", "postgres://localhost:5432/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355");
            var  logrFactory = new LoggerFactory();
            var  connFactory = new PostgresProviderConnectorFactory(sInfo, sqlConfig, implementationType);
            var  h           = new RelationalHealthContributor((IDbConnection)connFactory.Create(null), logrFactory.CreateLogger <RelationalHealthContributor>());

            // act
            var status = h.Health();

            // assert
            Assert.Equal(HealthStatus.DOWN, status.Status);
            Assert.Contains(status.Details.Keys, k => k == "error");
        }
        private static NpgsqlConnection GetConnection(IConfiguration config, string serviceName = null)
        {
            PostgresServiceInfo info = null;

            if (string.IsNullOrEmpty(serviceName))
            {
                info = config.GetSingletonServiceInfo <PostgresServiceInfo>();
            }
            else
            {
                info = config.GetRequiredServiceInfo <PostgresServiceInfo>(serviceName);
            }

            PostgresProviderConnectorOptions postgresConfig = new PostgresProviderConnectorOptions(config);

            PostgresProviderConnectorFactory factory = new PostgresProviderConnectorFactory(info, postgresConfig);

            return(factory.Create(null) as NpgsqlConnection);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Adds NpgsqlConnection (as IDbConnection and NpgsqlConnection) 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> RegisterPostgreSqlConnection(this ContainerBuilder container, IConfiguration config, string serviceName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

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

            PostgresServiceInfo info = serviceName == null
                ? config.GetSingletonServiceInfo <PostgresServiceInfo>()
                : config.GetRequiredServiceInfo <PostgresServiceInfo>(serviceName);

            Type postgreSqlConnection = PostgreSqlTypeLocator.NpgsqlConnection;
            var  postgreSqlConfig     = new PostgresProviderConnectorOptions(config);
            var  factory = new PostgresProviderConnectorFactory(info, postgreSqlConfig, postgreSqlConnection);

            container.RegisterType <RelationalHealthContributor>().As <IHealthContributor>();
            return(container.Register(c => factory.Create(null)).As(typeof(IDbConnection), postgreSqlConnection));
        }