Represents an instance of a MongoDB server host.
 public void Setup()
 {
     _server = LegacyTestConfiguration.Server;
     _primary = _server.Instances.First(x => x.IsPrimary);
     _database = LegacyTestConfiguration.Database;
     _collection = LegacyTestConfiguration.Collection;
 }
 public void Setup()
 {
     _server = Configuration.TestServer;
     _primary = Configuration.TestServer.Primary;
     _database = Configuration.TestDatabase;
     _database.Drop();
 }
        /// <summary>
        /// Lets the server know that this thread is about to begin a series of related operations that must all occur
        /// on the same connection. The return value of this method implements IDisposable and can be placed in a
        /// using statement (in which case RequestDone will be called automatically when leaving the using statement).
        /// </summary>
        /// <param name="initialDatabase">One of the databases involved in the related operations.</param>
        /// <param name="serverInstance">The server instance this request should be tied to.</param>
        /// <returns>A helper object that implements IDisposable and calls <see cref="RequestDone"/> from the Dispose method.</returns>
        public virtual IDisposable RequestStart(MongoDatabase initialDatabase, MongoServerInstance serverInstance)
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            lock (_serverLock)
            {
                Request request;
                if (_requests.TryGetValue(threadId, out request))
                {
                    if (serverInstance != request.Connection.ServerInstance)
                    {
                        throw new InvalidOperationException("The server instance passed to a nested call to RequestStart does not match the server instance of the current Request.");
                    }
                    request.NestingLevel++;
                    return(new RequestStartResult(this));
                }
            }

            var connection = serverInstance.AcquireConnection();

            lock (_serverLock)
            {
                var request = new Request(connection);
                _requests.Add(threadId, request);
                return(new RequestStartResult(this));
            }
        }
 public void Setup()
 {
     _server = Configuration.TestServer;
     _primary = Configuration.TestServer.Primary;
     _database = Configuration.TestDatabase;
     _collection = Configuration.TestCollection;
 }
        internal MongoConnection AcquireConnection(MongoServerInstance serverInstance)
        {
            MongoConnection requestConnection = null;

            lock (_serverLock)
            {
                // if a thread has called RequestStart it wants all operations to take place on the same connection
                int     threadId = Thread.CurrentThread.ManagedThreadId;
                Request request;
                if (_requests.TryGetValue(threadId, out request))
                {
                    if (request.Connection.ServerInstance != serverInstance)
                    {
                        var message = string.Format(
                            "AcquireConnection called for server instance '{0}' but thread is in a RequestStart for server instance '{1}'.",
                            serverInstance.Address, request.Connection.ServerInstance.Address);
                        throw new MongoConnectionException(message);
                    }
                    requestConnection = request.Connection;
                }
            }

            if (requestConnection != null)
            {
                return(requestConnection);
            }

            return(serverInstance.AcquireConnection());
        }
Esempio n. 6
0
 // constructors
 public Request(ServerDescription serverDescription, MongoServerInstance serverInstance, IReadBindingHandle binding)
 {
     _serverDescription = serverDescription;
     _serverInstance    = serverInstance;
     _binding           = binding;
     _nestingLevel      = 1;
 }
Esempio n. 7
0
        internal MongoConnection AcquireConnection(
            MongoDatabase database,
            MongoServerInstance serverInstance
            )
        {
            // if a thread has called RequestStart it wants all operations to take place on the same connection
            int threadId = Thread.CurrentThread.ManagedThreadId;

            lock (requestsLock) {
                Request request;
                if (requests.TryGetValue(threadId, out request))
                {
                    if (request.Connection.ServerInstance != serverInstance)
                    {
                        var message = string.Format(
                            "AcquireConnection called for server instance '{0}' but thread is in a RequestStart for server instance '{1}'.",
                            serverInstance.Address,
                            request.Connection.ServerInstance.Address
                            );
                        throw new MongoConnectionException(message);
                    }
                    request.Connection.CheckAuthentication(database); // will throw exception if authentication fails
                    return(request.Connection);
                }
            }

            return(serverInstance.AcquireConnection(database));
        }
 public void Setup()
 {
     _server = Configuration.TestServer;
     _primary = _server.Instances.First(x => ReadPreference.Primary.MatchesInstance(x));
     _database = Configuration.TestDatabase;
     _collection = Configuration.TestCollection;
 }
 public void TestFixtureSetup()
 {
     _server = Configuration.TestServer;
     _adminDatabase = _server.GetDatabase("admin");
     _database = Configuration.TestDatabase;
     _primary = _server.Primary;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ServerInstanceConnectionProvider"/> class.
 /// </summary>
 /// <param name="serverInstance">The server instance.</param>
 /// <exception cref="System.ArgumentNullException">serverInstance</exception>
 public ServerInstanceConnectionProvider(MongoServerInstance serverInstance)
 {
     if (serverInstance == null)
     {
         throw new ArgumentNullException("serverInstance");
     }
     _serverInstance = serverInstance;
 }
 public void TestFixtureSetUp()
 {
     _primary = Configuration.TestServer.Primary;
     _collection = Configuration.TestCollection;
     _collectionName = Configuration.TestCollection.Name;
     _database = Configuration.TestDatabase;
     _databaseName = Configuration.TestDatabase.Name;
 }
Esempio n. 12
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ServerInstanceConnectionProvider"/> class.
 /// </summary>
 /// <param name="serverInstance">The server instance.</param>
 /// <exception cref="System.ArgumentNullException">serverInstance</exception>
 public ServerInstanceConnectionProvider(MongoServerInstance serverInstance)
 {
     if (serverInstance == null)
     {
         throw new ArgumentNullException("serverInstance");
     }
     _serverInstance = serverInstance;
 }
Esempio n. 13
0
        /// <summary>
        /// Lets the server know that this thread is about to begin a series of related operations that must all occur
        /// on the same connection. The return value of this method implements IDisposable and can be placed in a
        /// using statement (in which case RequestDone will be called automatically when leaving the using statement).
        /// </summary>
        /// <param name="serverInstance">The server instance this request should be tied to.</param>
        /// <returns>A helper object that implements IDisposable and calls <see cref="RequestDone"/> from the Dispose method.</returns>
        internal virtual IDisposable RequestStart(MongoServerInstance serverInstance)
        {
            var endPoint           = serverInstance.EndPoint;
            var serverSelector     = new EndPointServerSelector(endPoint);
            var coreReadPreference = serverInstance.GetServerDescription().Type.IsWritable() ? ReadPreference.Primary : ReadPreference.Secondary;

            return(RequestStart(serverSelector, coreReadPreference));
        }
 public void Setup()
 {
     _server = LegacyTestConfiguration.Server;
     _primary = LegacyTestConfiguration.Server.Primary;
     _database = LegacyTestConfiguration.Database;
     // TODO: DropDatabase
     //_database.Drop();
 }
Esempio n. 15
0
        /// <summary>
        /// Lets the server know that this thread is about to begin a series of related operations that must all occur
        /// on the same connection. The return value of this method implements IDisposable and can be placed in a
        /// using statement (in which case RequestDone will be called automatically when leaving the using statement).
        /// </summary>
        /// <param name="initialDatabase">One of the databases involved in the related operations.</param>
        /// <param name="serverInstance">The server instance this request should be tied to.</param>
        /// <returns>A helper object that implements IDisposable and calls <see cref="RequestDone"/> from the Dispose method.</returns>
        public virtual IDisposable RequestStart(MongoDatabase initialDatabase, MongoServerInstance serverInstance)
        {
            var address            = serverInstance.Address;
            var endPoint           = new DnsEndPoint(address.Host, address.Port);
            var serverSelector     = new EndPointServerSelector(endPoint);
            var coreReadPreference = serverInstance.GetServerDescription().Type.IsWritable() ? ReadPreference.Primary : ReadPreference.Secondary;

            return(RequestStart(serverSelector, coreReadPreference));
        }
 public MongoDatabaseTests()
 {
     _server = LegacyTestConfiguration.Server;
     _primary = LegacyTestConfiguration.Server.Primary;
     _database = LegacyTestConfiguration.Database;
     _adminDatabase = _server.GetDatabase("admin");
     // TODO: DropDatabase
     //_database.Drop();
 }
Esempio n. 17
0
 // constructors
 public Request(ServerDescription serverDescription, MongoServerInstance serverInstance, IReadBindingHandle binding, ConnectionId connectionId, IClientSessionHandle session)
 {
     _serverDescription = serverDescription;
     _serverInstance    = serverInstance;
     _binding           = binding;
     _connectionId      = connectionId;
     _session           = session;
     _nestingLevel      = 1;
 }
Esempio n. 18
0
 internal void RemoveInstance(
     MongoServerInstance instance
     )
 {
     lock (instances) {
         instance.StateChanged -= InstanceStateChanged;
         instances.Remove(instance);
         InstanceStateChanged(null, null); // removing an instance can change server state
     }
 }
        /// <summary>
        /// Tests whether the server instance matches the read preference.
        /// </summary>
        /// <param name="instance">The server instance.</param>
        /// <returns>True if the server instance matches the read preferences.</returns>
        public bool MatchesInstance(MongoServerInstance instance)
        {
            switch (_readPreferenceMode)
            {
            case ReadPreferenceMode.Primary:
                if (!instance.IsPrimary)
                {
                    return(false);
                }
                break;

            case ReadPreferenceMode.Secondary:
                if (!instance.IsSecondary)
                {
                    return(false);
                }
                break;

            case ReadPreferenceMode.PrimaryPreferred:
            case ReadPreferenceMode.SecondaryPreferred:
            case ReadPreferenceMode.Nearest:
                if (!instance.IsPrimary && !instance.IsSecondary)
                {
                    return(false);
                }
                break;

            default:
                throw new MongoInternalException("Invalid ReadPreferenceMode");
            }

            if (_tagSets != null && instance.InstanceType == MongoServerInstanceType.ReplicaSetMember)
            {
                var someSetMatches = false;
                foreach (var tagSet in _tagSets)
                {
                    if (tagSet.MatchesInstance(instance))
                    {
                        someSetMatches = true;
                        break;
                    }
                }

                if (!someSetMatches)
                {
                    return(false);
                }
            }

            return(true);
        }
 private MongoConnection AcquireConnection()
 {
     if (serverInstance == null)
     {
         // first time we need a connection let Server.AcquireConnection pick the server instance
         var connection = cursor.Server.AcquireConnection(cursor.Database, cursor.SlaveOk);
         serverInstance = connection.ServerInstance;
         return(connection);
     }
     else
     {
         // all subsequent requests for the same cursor must go to the same server instance
         return(cursor.Server.AcquireConnection(cursor.Database, serverInstance));
     }
 }
 // private methods
 private MongoConnection AcquireConnection()
 {
     if (_serverInstance == null)
     {
         // first time we need a connection let Server.AcquireConnection pick the server instance
         var connection = _cursor.Server.AcquireConnection(_readPreference);
         _serverInstance = connection.ServerInstance;
         return(connection);
     }
     else
     {
         // all subsequent requests for the same cursor must go to the same server instance
         return(_cursor.Server.AcquireConnection(_serverInstance));
     }
 }
Esempio n. 22
0
 internal void AddInstance(
     MongoServerInstance instance
     )
 {
     lock (instances) {
         if (instances.Any(i => i.Address == instance.Address))
         {
             var message = string.Format("A server instance already exists for address: {0}", instance.Address);
             throw new ArgumentException(message);
         }
         instances.Add(instance);
         instance.StateChanged += InstanceStateChanged;
         InstanceStateChanged(null, null); // adding an instance can change server state
     }
 }
Esempio n. 23
0
        // constructors
        public FailPoint(string name, MongoServer server, MongoServerInstance serverInstance)
        {
            if (name == null) { throw new ArgumentNullException("name"); }
            if (server == null) { throw new ArgumentNullException("server"); }
            if (serverInstance == null) { throw new ArgumentNullException("serverInstance"); }

            if (server.RequestConnection != null)
            {
                throw new InvalidOperationException("FailPoint cannot be used when you are already in a RequestStart.");
            }

            _name = name;
            _server = server;
            _serverInstance = serverInstance;
            _adminDatabase = server.GetDatabase("admin");
            _request = server.RequestStart(_adminDatabase, serverInstance);
        }
        private TCommandResult RunWriteCommandAs <TCommandResult>(
            BsonDocument command,
            IBsonSerializer <TCommandResult> resultSerializer,
            out MongoServerInstance serverInstance)
            where TCommandResult : CommandResult
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new WriteCommandOperation <TCommandResult>(new DatabaseNamespace(_name), command, resultSerializer, messageEncoderSettings);

            using (var binding = _server.GetWriteBinding())
                using (var connectionSource = binding.GetWriteConnectionSource())
                {
                    var endPoint = (DnsEndPoint)connectionSource.ServerDescription.EndPoint;
                    var address  = new MongoServerAddress(endPoint.Host, endPoint.Port);
                    serverInstance = _server.GetServerInstance(address);
                    return(operation.Execute(connectionSource, Timeout.InfiniteTimeSpan, CancellationToken.None));
                }
        }
Esempio n. 25
0
        /// <summary>
        /// Tests whether this tag set matches a server instance.
        /// </summary>
        /// <param name="instance">The server instance.</param>
        /// <returns>True if every tag in this tag set is also in the server instance tag set; otherwise, false.</returns>
        public bool MatchesInstance(MongoServerInstance instance)
        {
            // an empty tag set matches anything
            if (instance.InstanceType != MongoServerInstanceType.ReplicaSetMember || _tags.Count == 0)
            {
                return(true);
            }

            var tagSet = instance.ReplicaSetInformation.TagSet;

            foreach (var tag in _tags)
            {
                if (!tagSet.Contains(tag))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 26
0
        // internal methods
        internal MongoServerInstance ChooseServerInstance(IEnumerable <MongoServerInstance> connectedInstancesByPingTime)
        {
            // tags are not evaluated for a primary
            if (_readPreferenceMode == ReadPreferenceMode.Primary || _readPreferenceMode == ReadPreferenceMode.PrimaryPreferred)
            {
                foreach (var instance in connectedInstancesByPingTime)
                {
                    if (instance.IsPrimary)
                    {
                        return(instance);
                    }
                }
                if (_readPreferenceMode == ReadPreferenceMode.Primary)
                {
                    return(null);
                }
            }

            List <MongoServerInstance> matchingInstances = new List <MongoServerInstance>();
            TimeSpan maxPingTime = TimeSpan.MaxValue;

            foreach (var instance in connectedInstancesByPingTime)
            {
                if (instance.AveragePingTime > maxPingTime)
                {
                    break; // any subsequent instances will also exceed maxPingTime
                }
                if (MatchesInstance(instance))
                {
                    if (maxPingTime == TimeSpan.MaxValue)
                    {
                        var secondaryAcceptableLatency = TimeSpan.FromMilliseconds(15);
                        try
                        {
                            maxPingTime = instance.AveragePingTime + secondaryAcceptableLatency;
                        }
                        catch (OverflowException)
                        {
                            maxPingTime = TimeSpan.MaxValue;
                        }
                    }
                    matchingInstances.Add(instance);
                }
            }

            if (matchingInstances.Count == 0)
            {
                return(null);
            }

            if (_readPreferenceMode == ReadPreferenceMode.SecondaryPreferred)
            {
                MongoServerInstance primary = null;
                foreach (var instance in matchingInstances)
                {
                    if (instance.IsPrimary)
                    {
                        primary = instance;
                        break;
                    }
                }

                if (primary != null)
                {
                    if (matchingInstances.Count == 1)
                    {
                        return(primary);
                    }
                    else
                    {
                        matchingInstances.Remove(primary);
                    }
                }
            }

            switch (matchingInstances.Count)
            {
            case 0:
                return(null);

            case 1:
                return(matchingInstances[0]);

            default:
                int randomIndex;
                lock (_randomLock)
                {
                    randomIndex = _random.Next(matchingInstances.Count);
                }
                return(matchingInstances[randomIndex]);    // random load balancing
            }
        }
Esempio n. 27
0
 public void OneTimeSetUp()
 {
     _server = LegacyTestConfiguration.Server;
     _database = LegacyTestConfiguration.Database;
     _primary = _server.Primary;
 }
 /// <summary>
 /// 获取实例节点
 /// </summary>
 /// <param name="mongoConnKey"></param>
 /// <param name="config">由于是结构体,必须ref</param>
 /// <param name="mongoConn"></param>
 /// <param name="mMasterServerInstace"></param>
 /// <param name="mServer"></param>
 /// <param name="UserList"></param>
 /// <returns></returns>
 private static TreeNode GetInstanceNode(String mongoConnKey,
                                         ref ConfigHelper.MongoConnectionConfig config,
                                         MongoServer mongoConn,
                                         MongoServerInstance mMasterServerInstace,
                                         MongoServer mServer,
                                         EachDatabaseUser UserList)
 {
     Boolean isReplsetMasterServer = false;
     //无论如何,都改为主要服务器读优先
     if (mMasterServerInstace == null)
     {
         isReplsetMasterServer = true;
     }
     TreeNode SvrInstanceNode = new TreeNode();
     String ConnSvrKey;
     if (isReplsetMasterServer)
     {
         ConnSvrKey = mongoConnKey + "/" + mongoConnKey;
     }
     else
     {
         ConnSvrKey = mongoConnKey + "/" + mMasterServerInstace.Address.ToString().Replace(":", "@");
     }
     SvrInstanceNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer;
     SvrInstanceNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer;
     if (isReplsetMasterServer)
     {
         SvrInstanceNode.Text = "Connection";
     }
     else
     {
         SvrInstanceNode.Text = "Server[" + mMasterServerInstace.Address.ToString() + "]";
     }
     if ((!String.IsNullOrEmpty(config.UserName)) & (!String.IsNullOrEmpty(config.Password)))
     {
         //是否是认证模式,应该取决于服务器!
         config.AuthMode = true;
     }
     //获取ReadOnly
     config.IsReadOnly = false;
     List<String> databaseNameList = new List<String>();
     if (!String.IsNullOrEmpty(config.DataBaseName))
     {
         //单数据库模式
         TreeNode mongoSingleDBNode;
         if (isReplsetMasterServer)
         {
             mongoSingleDBNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mServer, mongoConnKey + "/" + mongoConnKey);
         }
         else
         {
             mongoSingleDBNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mServer, mongoConnKey + "/" + mMasterServerInstace.Address.ToString());
         }
         mongoSingleDBNode.Tag = SINGLE_DATABASE_TAG + ":" + ConnSvrKey + "/" + config.DataBaseName;
         mongoSingleDBNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
         mongoSingleDBNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
         SvrInstanceNode.Nodes.Add(mongoSingleDBNode);
         SvrInstanceNode.Tag = SINGLE_DB_SERVER_TAG + ":" + ConnSvrKey;
         ///获取User信息
         if (config.AuthMode)
         {
             try
             {
                 ///尝试添加用户信息
                 UserList.AddUser(mongoConn.GetDatabase(config.DataBaseName), config.UserName);
             }
             catch (Exception)
             {
                 //可能出现没有权限的问题,这里就认为无法取得权限
             }
         }
     }
     else
     {
         MongoServer InstantSrv;
         if (isReplsetMasterServer)
         {
             InstantSrv = mServer;
             databaseNameList = mServer.GetDatabaseNames().ToList<String>();
         }
         else
         {
             MongoClientSettings setting = CreateMongoClientSettingsByConfig(ref config);
             setting.ConnectionMode = ConnectionMode.Direct;
             //When Replset Case,Application need to read admin DB information
             //if Primary,there will be exception
             setting.ReadPreference = ReadPreference.PrimaryPreferred;
             setting.Server = mMasterServerInstace.Address;
             InstantSrv = new MongoClient(setting).GetServer();
             databaseNameList = InstantSrv.GetDatabaseNames().ToList<String>();
         }
         foreach (String strDBName in databaseNameList)
         {
             TreeNode mongoDBNode;
             try
             {
                 mongoDBNode = FillDataBaseInfoToTreeNode(strDBName, InstantSrv, ConnSvrKey);
                 mongoDBNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 mongoDBNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 SvrInstanceNode.Nodes.Add(mongoDBNode);
                 if (config.AuthMode)
                 {
                     try
                     {
                         ///尝试添加用户信息
                         UserList.AddUser(mongoConn.GetDatabase(strDBName), config.UserName);
                     }
                     catch (Exception)
                     {
                         //可能出现没有权限的问题,这里就认为无法取得权限
                     }
                 }
             }
             catch (Exception ex)
             {
                 SystemManager.ExceptionDeal(ex, strDBName + "Exception", strDBName + "Exception");
                 mongoDBNode = new TreeNode(strDBName + " (Exception)");
                 mongoDBNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 mongoDBNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 SvrInstanceNode.Nodes.Add(mongoDBNode);
             }
         }
         if (isReplsetMasterServer)
         {
             SvrInstanceNode.Tag = SERVER_TAG + ":" + mongoConnKey + "/" + mongoConnKey;
         }
         else
         {
             if (mongoConn.ReplicaSetName != null)
             {
                 SvrInstanceNode.Tag = SERVER_REPLSET_MEMBER_TAG + ":" + mongoConnKey + "/" + mMasterServerInstace.Address.ToString().Replace(":", "@");
             }
         }
     }
     if (_mongoInstanceLst.ContainsKey(ConnSvrKey))
     {
         _mongoInstanceLst.Remove(ConnSvrKey);
     }
     if (!isReplsetMasterServer)
     {
         _mongoInstanceLst.Add(ConnSvrKey, mMasterServerInstace);
     }
     return SvrInstanceNode;
 }
Esempio n. 29
0
 public IndexKeysBuilderTests()
 {
     _server = LegacyTestConfiguration.Server;
     _database = LegacyTestConfiguration.Database;
     _primary = _server.Primary;
 }
 public void TestFixtureSetUp()
 {
     _server = Configuration.TestServer;
     _primary = _server.Instances.First(x => ReadPreference.Primary.MatchesInstance(x));
     _collection = Configuration.TestCollection;
 }
 public void TestFixtureSetup()
 {
     _server = LegacyTestConfiguration.Server;
     _database = LegacyTestConfiguration.Database;
     _primary = _server.Primary;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="mongoConnKey"></param>
 /// <param name="config"></param>
 /// <param name="mongoConn"></param>
 /// <param name="mServerInstace"></param>
 /// <param name="mServer"></param>
 /// <returns></returns>
 private static TreeNode GetInstanceNode(String mongoConnKey, ConfigHelper.MongoConnectionConfig config,
                                         MongoServer mongoConn, MongoServerInstance mServerInstace, MongoServer mServer)
 {
     Boolean isServer = false;
     if (mServerInstace == null)
     {
         isServer = true;
     }
     TreeNode SvrInstanceNode = new TreeNode();
     String ConnSvrKey;
     if (isServer)
     {
         ConnSvrKey = mongoConnKey + "/" + mongoConnKey;
     }
     else
     {
         ConnSvrKey = mongoConnKey + "/" + mServerInstace.Address.ToString().Replace(":", "@");
     }
     SvrInstanceNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer;
     SvrInstanceNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer;
     if (isServer)
     {
         SvrInstanceNode.Text = "Connection";
     }
     else
     {
         SvrInstanceNode.Text = "Server[" + mServerInstace.Address.ToString() + "]";
     }
     if ((!String.IsNullOrEmpty(config.UserName)) & (!String.IsNullOrEmpty(config.Password)))
     {
         config.AuthMode = true;
     }
     //获取ReadOnly
     config.IsReadOnly = false;
     List<String> databaseNameList = new List<String>();
     if (!String.IsNullOrEmpty(config.DataBaseName))
     {
         //单数据库模式
         TreeNode mongoSingleDBNode;
         if (isServer)
         {
             mongoSingleDBNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mServer, mongoConnKey + "/" + mongoConnKey);
         }
         else
         {
             mongoSingleDBNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mServerInstace.Server, mongoConnKey + "/" + mServerInstace.Address.ToString());
         }
         mongoSingleDBNode.Tag = SINGLE_DATABASE_TAG + ":" + ConnSvrKey + "/" + config.DataBaseName;
         mongoSingleDBNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
         mongoSingleDBNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
         SvrInstanceNode.Nodes.Add(mongoSingleDBNode);
         SvrInstanceNode.Tag = SINGLE_DB_SERVER_TAG + ":" + ConnSvrKey;
         if (config.AuthMode)
         {
             config.IsReadOnly = mongoConn.GetDatabase(config.DataBaseName).FindUser(config.UserName).IsReadOnly;
         }
     }
     else
     {
         MongoServer InstantSrv;
         if (isServer)
         {
             InstantSrv = mServer;
             databaseNameList = mServer.GetDatabaseNames().ToList<String>();
         }
         else
         {
             MongoServerSettings setting = mongoConn.Settings.Clone();
             setting.ConnectionMode = ConnectionMode.Direct;
             //When Replset Case,Application need to read admin DB information
             //if Primary,there will be exception
             setting.ReadPreference = ReadPreference.PrimaryPreferred;
             setting.Server = mServerInstace.Address;
             InstantSrv = new MongoServer(setting);
             databaseNameList = InstantSrv.GetDatabaseNames().ToList<String>();
         }
         foreach (String strDBName in databaseNameList)
         {
             TreeNode mongoDBNode;
             try
             {
                 mongoDBNode = FillDataBaseInfoToTreeNode(strDBName, InstantSrv, ConnSvrKey);
                 mongoDBNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 mongoDBNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 SvrInstanceNode.Nodes.Add(mongoDBNode);
                 if (strDBName == MongoDBHelper.DATABASE_NAME_ADMIN)
                 {
                     if (config.AuthMode)
                     {
                         config.IsReadOnly = mongoConn.GetDatabase(strDBName).FindUser(config.UserName).IsReadOnly;
                     }
                 }
             }
             catch (Exception ex)
             {
                 MyMessageBox.ShowMessage(strDBName + "Exception", strDBName + "Exception", ex.ToString());
                 mongoDBNode = new TreeNode(strDBName + " (Exception)");
                 mongoDBNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 mongoDBNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
                 SvrInstanceNode.Nodes.Add(mongoDBNode);
             }
         }
         if (isServer)
         {
             SvrInstanceNode.Tag = SERVER_TAG + ":" + mongoConnKey + "/" + mongoConnKey;
         }
         else
         {
             if (mongoConn.ReplicaSetName != null)
             {
                 SvrInstanceNode.Tag = SERVER_REPLSET_MEMBER_TAG + ":" + mongoConnKey + "/" + mServerInstace.Address.ToString().Replace(":", "@");
             }
         }
     }
     if (_mongoInstanceLst.ContainsKey(ConnSvrKey))
     {
         _mongoInstanceLst.Remove(ConnSvrKey);
     }
     if (!isServer)
     {
         _mongoInstanceLst.Add(ConnSvrKey, mServerInstace);
     }
     return SvrInstanceNode;
 }
 public void TestFixtureSetUp()
 {
     _primary = Configuration.TestServer.Primary;
     _collection = Configuration.TestCollection;
 }