예제 #1
0
 public void TestSafeModeTrueFSyncTrueW2WTimeout()
 {
     string connectionString = "mongodb://localhost/?safe=true;fsync=true;w=2;wtimeout=2s";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(SafeMode.Create(true, true, 2, TimeSpan.FromSeconds(2)), url.SafeMode);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #2
0
        public static IMongoDatabase Connect()
        {
            var         connectionString = new MongoUrl("");
            MongoClient client           = new MongoClient(connectionString.ToString());

            return(client.GetDatabase(connectionString.DatabaseName));
        }
예제 #3
0
 public void TestSafeModeFSyncFalse()
 {
     string connectionString = "mongodb://localhost/?safe=true;fsync=false";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(SafeMode.True, url.SafeMode);
     Assert.AreEqual("mongodb://localhost/?safe=true", url.ToString()); // fsync=false dropped
 }
예제 #4
0
        private static void Main(string[] args)
        {
            var storageOptions = new MongoStorageOptions
            {
                MigrationOptions = new MongoMigrationOptions
                {
                    MigrationStrategy = new MigrateMongoMigrationStrategy(),
                    BackupStrategy    = new NoneMongoBackupStrategy()
                }
            };
            var url = new MongoUrl("mongodb://*****:*****@localhost/jarvis-hangfire-test?authSource=admin");

            GlobalConfiguration
            .Configuration
            .UseMongoStorage(
                url.ToString(),
                storageOptions);

            string command;

            do
            {
                Console.Write("Insert Command:");
                command = Console.ReadLine();
                ExecuteCommand(command);
            } while (!string.IsNullOrEmpty(command));
        }
예제 #5
0
 public void TestSafeModeTrue()
 {
     string connectionString = "mongodb://localhost/?safe=true";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(SafeMode.True, url.SafeMode);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #6
0
 public void TestMaxConnectionLifeTime()
 {
     string connectionString = "mongodb://localhost/?maxLifeTime=123ms";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(TimeSpan.FromMilliseconds(123), url.MaxConnectionLifeTime);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #7
0
 public void TestMinConnectionPoolSize()
 {
     string connectionString = "mongodb://localhost/?minPoolSize=123";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(123, url.MinConnectionPoolSize);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #8
0
 public void TestGuidRepresentationJavaLegacy()
 {
     string connectionString = "mongodb://localhost/?guids=JavaLegacy";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(GuidRepresentation.JavaLegacy, url.GuidRepresentation);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #9
0
 public void TestGuidRepresentationCSharpLegacy()
 {
     string connectionString = "mongodb://localhost/?guids=CSharpLegacy";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(GuidRepresentation.CSharpLegacy, url.GuidRepresentation);
     Assert.AreEqual("mongodb://localhost", url.ToString()); // guids=CSharpLegacy dropped
 }
예제 #10
0
 public void TestIpV6()
 {
     string connectionString = "mongodb://localhost/?ipv6=true";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(true, url.IPv6);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #11
0
        public void TestNoMaxStaleness(string value)
        {
            var url = new MongoUrl(value);

            url.ReadPreference.MaxStaleness.Should().NotHaveValue();
            url.ToString().Should().Be("mongodb://localhost/?readPreference=secondary");
        }
예제 #12
0
 public void TestSafeModeW2()
 {
     string connectionString = "mongodb://localhost/?w=2";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(SafeMode.W2, url.SafeMode);
     Assert.AreEqual("mongodb://localhost/?safe=true;w=2", url.ToString()); // safe=true added
 }
예제 #13
0
 public void TestSlaveOkFalse()
 {
     string connectionString = "mongodb://localhost/?slaveOk=false";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(false, url.SlaveOk);
     Assert.AreEqual("mongodb://localhost", url.ToString()); // slaveOk=false dropped
 }
예제 #14
0
        public void TestDefaults()
        {
            string   connectionString = "mongodb://localhost";
            MongoUrl url = new MongoUrl(connectionString);

            Assert.IsNull(url.DefaultCredentials);
            Assert.AreEqual(1, url.Servers.Count());
            Assert.AreEqual("localhost", url.Servers.Single().Host);
            Assert.AreEqual(27017, url.Servers.Single().Port);
            Assert.IsNull(url.DatabaseName);
            Assert.AreEqual(ConnectionMode.Automatic, url.ConnectionMode);
            Assert.AreEqual(MongoDefaults.ConnectTimeout, url.ConnectTimeout);
            Assert.AreEqual(MongoDefaults.GuidRepresentation, url.GuidRepresentation);
            Assert.AreEqual(false, url.IPv6);
            Assert.AreEqual(MongoDefaults.MaxConnectionIdleTime, url.MaxConnectionIdleTime);
            Assert.AreEqual(MongoDefaults.MaxConnectionLifeTime, url.MaxConnectionLifeTime);
            Assert.AreEqual(MongoDefaults.MaxConnectionPoolSize, url.MaxConnectionPoolSize);
            Assert.AreEqual(null, url.ReplicaSetName);
            Assert.AreEqual(SafeMode.False, url.SafeMode);
#pragma warning disable 618
            Assert.AreEqual(false, url.SlaveOk);
#pragma warning restore
            Assert.AreEqual(MongoDefaults.SocketTimeout, url.SocketTimeout);
            Assert.AreEqual(false, url.UseSsl);
            Assert.AreEqual(MongoDefaults.WaitQueueMultiple, url.WaitQueueMultiple);
            Assert.AreEqual(MongoDefaults.WaitQueueSize, url.WaitQueueSize);
            Assert.AreEqual(MongoDefaults.WaitQueueTimeout, url.WaitQueueTimeout);
            Assert.AreEqual(connectionString, url.ToString());
        }
예제 #15
0
        public void TestConnectTimeout()
        {
            string   connectionString = "mongodb://localhost/?connectTimeout=12";
            MongoUrl url = new MongoUrl(connectionString);

            Assert.AreEqual(TimeSpan.FromSeconds(12), url.ConnectTimeout);
            Assert.AreEqual(connectionString + "s", url.ToString()); // "s" units added

            connectionString = "mongodb://localhost/?connectTimeout=123ms";
            url = new MongoUrl(connectionString);
            Assert.AreEqual(TimeSpan.FromMilliseconds(123), url.ConnectTimeout);
            Assert.AreEqual(connectionString, url.ToString());

            connectionString = "mongodb://localhost/?connectTimeout=12s";
            url = new MongoUrl(connectionString);
            Assert.AreEqual(TimeSpan.FromSeconds(12), url.ConnectTimeout);
            Assert.AreEqual(connectionString, url.ToString());

            connectionString = "mongodb://localhost/?connectTimeout=12m";
            url = new MongoUrl(connectionString);
            Assert.AreEqual(TimeSpan.FromMinutes(12), url.ConnectTimeout);
            Assert.AreEqual(connectionString, url.ToString());

            connectionString = "mongodb://localhost/?connectTimeout=12h";
            url = new MongoUrl(connectionString);
            Assert.AreEqual(TimeSpan.FromHours(12), url.ConnectTimeout);
            Assert.AreEqual(connectionString, url.ToString());

            connectionString = "mongodb://localhost/?connectTimeoutMS=123";
            url = new MongoUrl(connectionString);
            Assert.AreEqual(TimeSpan.FromMilliseconds(123), url.ConnectTimeout);
            Assert.AreEqual("mongodb://localhost/?connectTimeout=123ms", url.ToString()); // changed to "ms" suffix
        }
예제 #16
0
 public void TestSlaveOkTrue()
 {
     string connectionString = "mongodb://localhost/?slaveOk=true";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(true, url.SlaveOk);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #17
0
 public void TestWaitQueueTimeout()
 {
     string connectionString = "mongodb://localhost/?waitQueueTimeout=123ms";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(TimeSpan.FromMilliseconds(123), url.WaitQueueTimeout);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #18
0
 public void TestSafeModeTrueFSyncTrueW2()
 {
     string connectionString = "mongodb://localhost/?safe=true;fsync=true;w=2";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(SafeMode.Create(true, true, 2), url.SafeMode);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #19
0
 public void TestWaitQueueSize()
 {
     string connectionString = "mongodb://localhost/?waitQueueSize=123";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual(0, url.WaitQueueMultiple);
     Assert.AreEqual(123, url.WaitQueueSize);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #20
0
        public Entity()
        {
            var mongoUrl = new MongoUrl(AppSettingHelper.GetStringFromFileJson("connectstring", "ConnectionStrings:DefaultConnection").Result);

            _MongoClient = new MongoClient(mongoUrl.ToString().Replace(@"/" + mongoUrl.DatabaseName, ""));

            _MongoDatabase = _MongoClient.GetDatabase(mongoUrl.DatabaseName);
        }
예제 #21
0
        public void TestSslDontVerifyCertificate()
        {
            string   connectionString = "mongodb://localhost/?ssl=true;sslVerifyCertificate=false";
            MongoUrl url = new MongoUrl(connectionString);

            Assert.AreEqual(true, url.UseSsl);
            Assert.AreEqual(false, url.VerifySslCertificate);
            Assert.AreEqual(connectionString, url.ToString());
        }
예제 #22
0
        public void TestReplicaSetName()
        {
            string   connectionString = "mongodb://localhost/?replicaSet=name";
            MongoUrl url = new MongoUrl(connectionString);

            Assert.AreEqual(ConnectionMode.Automatic, url.ConnectionMode);
            Assert.AreEqual("name", url.ReplicaSetName);
            Assert.AreEqual("mongodb://localhost/?replicaSet=name", url.ToString());
        }
예제 #23
0
        public DbContext()
        {
            var url      = ConfigurationManager.AppSettings.Get("MONGOLAB_URI");
            var mongoUrl = new MongoUrl(url);

            Trace.TraceInformation("Connecting to MongoDB at " + mongoUrl.ToString());
            _client = new MongoClient(mongoUrl);
            _db     = _client.GetDatabase(mongoUrl.DatabaseName);
            SeedWithInitialData();
        }
예제 #24
0
        public Task <bool> Start()
        {
            _mongoClient = new MongoClient(_mongoUrl.ToString());
            _logger.LogInformation($"Connecting to {_mongoUrl.DatabaseName}");
            _mongoDatabase = _mongoClient.GetDatabase(_mongoUrl.DatabaseName);
            _mongoClient.ListDatabases().ToList();
            _logger.LogInformation($"Connected {_mongoUrl.DatabaseName}");

            return(Task.FromResult(true));
        }
예제 #25
0
        public void TestSlaveOkTrue()
        {
            string   connectionString = "mongodb://localhost/?slaveOk=true";
            MongoUrl url = new MongoUrl(connectionString);

#pragma warning disable 618
            Assert.AreEqual(true, url.SlaveOk);
#pragma warning restore
            Assert.AreEqual(connectionString, url.ToString());
        }
        /// <summary>
        /// Get mongo client by Mongo connection URL
        /// </summary>
        /// <param name="mongoUrl">Mongo connection Url</param>
        /// <returns></returns>
        public static IMongoClient Get(MongoUrl mongoUrl)
        {
            var mongoUrlStr = mongoUrl.ToString();

            if (!_clients.ContainsKey(mongoUrlStr))
            {
                _clients.Add(mongoUrlStr, new MongoClient(mongoUrl));
            }

            return(_clients[mongoUrlStr]);
        }
예제 #27
0
        public void TestConnectionMode()
        {
            string connectionString = "mongodb://localhost/?connect=direct";
            MongoUrl url = new MongoUrl(connectionString);
            Assert.AreEqual(ConnectionMode.Direct, url.ConnectionMode);
            Assert.AreEqual("mongodb://localhost", url.ToString()); // connect=direct dropped

            connectionString = "mongodb://localhost/?connect=replicaSet";
            url = new MongoUrl(connectionString);
            Assert.AreEqual(ConnectionMode.ReplicaSet, url.ConnectionMode);
            Assert.AreEqual(connectionString, url.ToString());
        }
예제 #28
0
 public void TestHostWithPort()
 {
     string connectionString = "mongodb://mongo.xyz.com:12345";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.IsNull(url.DefaultCredentials);
     Assert.AreEqual(1, url.Servers.Count());
     Assert.AreEqual("mongo.xyz.com", url.Servers.Single().Host);
     Assert.AreEqual(12345, url.Servers.Single().Port);
     Assert.AreEqual(ConnectionMode.Direct, url.ConnectionMode);
     Assert.AreEqual(null, url.ReplicaSetName);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #29
0
 public void TestUsernamePasswordLocalhostDatabase()
 {
     string connectionString = "mongodb://*****:*****@localhost/database";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.AreEqual("username", url.DefaultCredentials.Username);
     Assert.AreEqual("password", url.DefaultCredentials.Password);
     Assert.AreEqual(1, url.Servers.Count());
     Assert.AreEqual("localhost", url.Servers.Single().Host);
     Assert.AreEqual(27017, url.Servers.Single().Port);
     Assert.AreEqual("database", url.DatabaseName);
     Assert.AreEqual(ConnectionMode.Direct, url.ConnectionMode);
     Assert.AreEqual(null, url.ReplicaSetName);
     Assert.AreEqual(connectionString, url.ToString());
 }
예제 #30
0
 public void TestTwoHosts()
 {
     string connectionString = "mongodb://mongo1.xyz.com,mongo2.xyz.com";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.IsNull(url.DefaultCredentials);
     Assert.AreEqual(2, url.Servers.Count());
     Assert.AreEqual("mongo1.xyz.com", url.Servers.First().Host);
     Assert.AreEqual(27017, url.Servers.First().Port);
     Assert.AreEqual("mongo2.xyz.com", url.Servers.Skip(1).Single().Host);
     Assert.AreEqual(27017, url.Servers.Skip(1).Single().Port);
     Assert.AreEqual(ConnectionMode.ReplicaSet, url.ConnectionMode);
     Assert.AreEqual(null, url.ReplicaSetName);
     Assert.AreEqual(connectionString, url.ToString());
 }