Exemplo n.º 1
0
        public void TestReadConfig()
        {
            var parameters = ConfigParams.FromTuples(
                "param1", "Test Param 1",
                "param2", "Test Param 2");

            ConfigParams config = JsonConfigReader.ReadConfig(null, "../../../../data/config.json", parameters);

            Assert.Equal(9, config.Count);
            Assert.Equal(123, config.GetAsInteger("field1.field11"));
            Assert.Equal("ABC", config.GetAsString("field1.field12"));
            Assert.Equal(123, config.GetAsInteger("field2.0"));
            Assert.Equal("ABC", config.GetAsString("field2.1"));
            Assert.Equal(543, config.GetAsInteger("field2.2.field21"));
            Assert.Equal("XYZ", config.GetAsString("field2.2.field22"));
            Assert.Equal(true, config.GetAsBoolean("field3"));
        }
        public async Task OpenAsync(string correlationId, ConnectionParams connection, CredentialParams credential)
        {
            if (connection == null)
            {
                throw new ConfigException(correlationId, "NO_CONNECTION", "Database connection is not set");
            }

            var uri          = connection.Uri;
            var host         = connection.Host;
            var port         = connection.Port;
            var databaseName = connection.GetAsNullableString("database");

            if (uri != null)
            {
                databaseName = MongoUrl.Create(uri).DatabaseName;
            }
            else
            {
                if (host == null)
                {
                    throw new ConfigException(correlationId, "NO_HOST", "Connection host is not set");
                }

                if (port == 0)
                {
                    throw new ConfigException(correlationId, "NO_PORT", "Connection port is not set");
                }

                if (databaseName == null)
                {
                    throw new ConfigException(correlationId, "NO_DATABASE", "Connection database is not set");
                }
            }

            _logger.Trace(correlationId, "Connecting to mongodb database {0}, collection {1}", databaseName, _collectionName);

            try
            {
                if (uri != null)
                {
                    _connection = new MongoClient(uri);
                }
                else
                {
                    var settings = new MongoClientSettings
                    {
                        Server = new MongoServerAddress(host, port),
                        MaxConnectionPoolSize = _options.GetAsInteger("poll_size"),
                        ConnectTimeout        = _options.GetAsTimeSpan("connect_timeout"),
                        //SocketTimeout =
                        //    new TimeSpan(options.GetInteger("server.socketOptions.socketTimeoutMS")*
                        //                 TimeSpan.TicksPerMillisecond)
                    };

                    if (credential.Username != null)
                    {
                        settings.Credential = MongoCredential.CreateCredential(databaseName, credential.Username, credential.Password);
                    }

                    _connection = new MongoClient(settings);
                }

                _database   = _connection.GetDatabase(databaseName);
                _collection = _database.GetCollection <T>(_collectionName);

                _logger.Debug(correlationId, "Connected to mongodb database {0}, collection {1}", databaseName, _collectionName);
            }
            catch (Exception ex)
            {
                throw new ConnectionException(correlationId, "ConnectFailed", "Connection to mongodb failed", ex);
            }

            await Task.Delay(0);
        }