当前位置: 首页>>代码示例>>C#>>正文


C# MongoConnection.RunCommand方法代码示例

本文整理汇总了C#中MongoConnection.RunCommand方法的典型用法代码示例。如果您正苦于以下问题:C# MongoConnection.RunCommand方法的具体用法?C# MongoConnection.RunCommand怎么用?C# MongoConnection.RunCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MongoConnection的用法示例。


在下文中一共展示了MongoConnection.RunCommand方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Authenticate

        // public methods
        /// <summary>
        /// Authenticates the connection against the given database.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="credential">The credential.</param>
        public void Authenticate(MongoConnection connection, MongoCredential credential)
        {
            var nonceCommand = new CommandDocument("getnonce", 1);
            var commandResult = connection.RunCommand(credential.Source, QueryFlags.None, nonceCommand, false);
            if (!commandResult.Ok)
            {
                throw new MongoAuthenticationException(
                    "Error getting nonce for authentication.",
                    new MongoCommandException(commandResult));
            }

            var nonce = commandResult.Response["nonce"].AsString;
            var passwordDigest = MongoUtils.Hash(credential.Username + ":mongo:" + ((PasswordEvidence)credential.Evidence).Password);
            var digest = MongoUtils.Hash(nonce + credential.Username + passwordDigest);
            var authenticateCommand = new CommandDocument
                {
                    { "authenticate", 1 },
                    { "user", credential.Username },
                    { "nonce", nonce },
                    { "key", digest }
                };

            commandResult = connection.RunCommand(credential.Source, QueryFlags.None, authenticateCommand, false);
            if (!commandResult.Ok)
            {
                var message = string.Format("Invalid credential for database '{0}'.", credential.Source);
                throw new MongoAuthenticationException(
                    message,
                    new MongoCommandException(commandResult));
            }
        }
开发者ID:wireclub,项目名称:mongo-csharp-driver,代码行数:37,代码来源:MongoCRAuthenticationProtocol.cs

示例2: Authenticate

        // public methods
        /// <summary>
        /// Authenticates the connection against the given database.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="credential">The credential.</param>
        public void Authenticate(MongoConnection connection, MongoCredential credential)
        {
            using (var conversation = new SaslConversation())
            {
                var currentStep = _mechanism.Initialize(connection, credential);

                var command = new CommandDocument
                {
                    { "saslStart", 1 },
                    { "mechanism", _mechanism.Name },
                    { "payload", currentStep.BytesToSendToServer }
                };

                while (true)
                {
                    CommandResult result;
                    try
                    {
                        result = connection.RunCommand(credential.Source, QueryFlags.SlaveOk, command, true);
                    }
                    catch (MongoCommandException ex)
                    {
                        var message = "Unknown error occured during authentication.";
                        var code = ex.CommandResult.Code;
                        var errmsg = ex.CommandResult.ErrorMessage;
                        if(code.HasValue && errmsg != null)
                        {
                            message = string.Format("Error: {0} - {1}", code, errmsg);
                        }

                        throw new MongoSecurityException(message, ex);
                    }

                    if (result.Response["done"].AsBoolean)
                    {
                        break;
                    }

                    currentStep = currentStep.Transition(conversation, result.Response["payload"].AsByteArray);

                    command = new CommandDocument
                    {
                        { "saslContinue", 1 },
                        { "conversationId", result.Response["conversationId"].AsInt32 },
                        { "payload", currentStep.BytesToSendToServer }
                    };
                }
            }
        }
开发者ID:wireclub,项目名称:mongo-csharp-driver,代码行数:55,代码来源:SaslAuthenticationProtocol.cs

示例3: Authenticate

        // public methods
        /// <summary>
        /// Authenticates the connection against the given database.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="credentials">The credentials.</param>
        public void Authenticate(MongoConnection connection, MongoCredentials credentials)
        {
            using (var conversation = new SaslConversation())
            {
                var currentStep = _mechanism.Initialize(connection, credentials);

                var command = new CommandDocument
                {
                    { "saslStart", 1 },
                    { "mechanism", _mechanism.Name },
                    { "payload", currentStep.BytesToSendToServer }
                };

                while (true)
                {
                    var result = connection.RunCommand(credentials.Source, QueryFlags.SlaveOk, command, true);
                    var code = result.Response["code"].AsInt32;
                    if (code != 0)
                    {
                        HandleError(result, code);
                    }
                    if (result.Response["done"].AsBoolean)
                    {
                        break;
                    }

                    currentStep = currentStep.Transition(conversation, result.Response["payload"].AsByteArray);

                    command = new CommandDocument
                    {
                        { "saslContinue", 1 },
                        { "conversationId", result.Response["conversationId"].AsInt32 },
                        { "payload", currentStep.BytesToSendToServer }
                    };
                }
            }
        }
开发者ID:petarvucetin,项目名称:mongo-csharp-driver,代码行数:43,代码来源:SaslAuthenticationMethod.cs

示例4: VerifyState

        internal void VerifyState(MongoConnection connection)
        {
            CommandResult isMasterResult = null;
            bool ok = false;
            try
            {
                var isMasterCommand = new CommandDocument("ismaster", 1);
                isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand, false);
                if (!isMasterResult.Ok)
                {
                    throw new MongoCommandException(isMasterResult);
                }

                var isPrimary = isMasterResult.Response["ismaster", false].ToBoolean();
                var isSecondary = isMasterResult.Response["secondary", false].ToBoolean();
                var isPassive = isMasterResult.Response["passive", false].ToBoolean();
                var isArbiter = isMasterResult.Response["arbiterOnly", false].ToBoolean();
                // workaround for CSHARP-273
                if (isPassive && isArbiter) { isPassive = false; }

                var maxDocumentSize = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32();
                var maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize

                MongoServerBuildInfo buildInfo;
                var buildInfoCommand = new CommandDocument("buildinfo", 1);
                var buildInfoResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, buildInfoCommand, false);
                if (buildInfoResult.Ok)
                {
                    buildInfo = new MongoServerBuildInfo(
                        buildInfoResult.Response["bits"].ToInt32(), // bits
                        buildInfoResult.Response["gitVersion"].AsString, // gitVersion
                        buildInfoResult.Response["sysInfo"].AsString, // sysInfo
                        buildInfoResult.Response["version"].AsString // versionString
                    );
                }
                else
                {
                    // short term fix: if buildInfo fails due to auth we don't know the server version; see CSHARP-324
                    if (buildInfoResult.ErrorMessage != "need to login")
                    {
                        throw new MongoCommandException(buildInfoResult);
                    }
                    buildInfo = null;
                }

                _isMasterResult = isMasterResult;
                _maxDocumentSize = maxDocumentSize;
                _maxMessageLength = maxMessageLength;
                _buildInfo = buildInfo;
                this.SetState(MongoServerState.Connected, isPrimary, isSecondary, isPassive, isArbiter);

                // if this is the primary of a replica set check to see if any instances have been added or removed
                if (isPrimary && _server.Settings.ConnectionMode == ConnectionMode.ReplicaSet)
                {
                    var instanceAddresses = new List<MongoServerAddress>();
                    if (isMasterResult.Response.Contains("hosts"))
                    {
                        foreach (var hostName in isMasterResult.Response["hosts"].AsBsonArray)
                        {
                            var address = MongoServerAddress.Parse(hostName.AsString);
                            instanceAddresses.Add(address);
                        }
                    }
                    if (isMasterResult.Response.Contains("passives"))
                    {
                        foreach (var hostName in isMasterResult.Response["passives"].AsBsonArray)
                        {
                            var address = MongoServerAddress.Parse(hostName.AsString);
                            instanceAddresses.Add(address);
                        }
                    }
                    if (isMasterResult.Response.Contains("arbiters"))
                    {
                        foreach (var hostName in isMasterResult.Response["arbiters"].AsBsonArray)
                        {
                            var address = MongoServerAddress.Parse(hostName.AsString);
                            instanceAddresses.Add(address);
                        }
                    }
                    _server.VerifyInstances(instanceAddresses);
                }

                ok = true;
            }
            finally
            {
                if (!ok)
                {
                    _isMasterResult = isMasterResult;
                    _maxDocumentSize = MongoDefaults.MaxDocumentSize;
                    _maxMessageLength = MongoDefaults.MaxMessageLength;
                    _buildInfo = null;
                    this.SetState(MongoServerState.Disconnected, false, false, false, false);
                }
            }
        }
开发者ID:ncipollina,项目名称:mongo-csharp-driver,代码行数:96,代码来源:MongoServerInstance.cs

示例5: Ping

 private void Ping(MongoConnection connection)
 {
     try
     {
         Stopwatch stopwatch = Stopwatch.StartNew();
         var pingCommand = new CommandDocument("ping", 1);
         connection.RunCommand("admin", QueryFlags.SlaveOk, pingCommand, true);
         stopwatch.Stop();
         var currentAverage = _pingTimeAggregator.Average;
         _pingTimeAggregator.Include(stopwatch.Elapsed);
         var newAverage = _pingTimeAggregator.Average;
         if (currentAverage != newAverage)
         {
             OnAveragePingTimeChanged();
         }
     }
     catch
     {
         _pingTimeAggregator.Clear();
         SetState(MongoServerState.Disconnected);
         throw;
     }
 }
开发者ID:kayone,项目名称:mongo-csharp-driver,代码行数:23,代码来源:MongoServerInstance.cs

示例6: LookupServerInformation

        // private methods
        private void LookupServerInformation(MongoConnection connection)
        {
            IsMasterResult isMasterResult = null;
            bool ok = false;
            try
            {
                var isMasterCommand = new CommandDocument("ismaster", 1);
                var tempResult = connection.RunCommand("admin", QueryFlags.SlaveOk, isMasterCommand, false);
                isMasterResult = new IsMasterResult();
                isMasterResult.Initialize(isMasterCommand, tempResult.Response);
                if (!isMasterResult.Ok)
                {
                    throw new MongoCommandException(isMasterResult);
                }

                MongoServerBuildInfo buildInfo;
                var buildInfoCommand = new CommandDocument("buildinfo", 1);
                var buildInfoResult = connection.RunCommand("admin", QueryFlags.SlaveOk, buildInfoCommand, false);
                if (buildInfoResult.Ok)
                {
                    buildInfo = MongoServerBuildInfo.FromCommandResult(buildInfoResult);
                }
                else
                {
                    // short term fix: if buildInfo fails due to auth we don't know the server version; see CSHARP-324
                    if (buildInfoResult.ErrorMessage != "need to login")
                    {
                        throw new MongoCommandException(buildInfoResult);
                    }
                    buildInfo = null;
                }

                ReplicaSetInformation replicaSetInformation = null;
                MongoServerInstanceType instanceType = MongoServerInstanceType.StandAlone;
                if (isMasterResult.ReplicaSetName != null)
                {
                    var tagSet = new ReplicaSetTagSet();
                    var peers = isMasterResult.Hosts.Concat(isMasterResult.Passives).Concat(isMasterResult.Arbiters).ToList();
                    replicaSetInformation = new ReplicaSetInformation(isMasterResult.ReplicaSetName, isMasterResult.Primary, peers, tagSet);
                    instanceType = MongoServerInstanceType.ReplicaSetMember;
                }
                else if (isMasterResult.Message != null && isMasterResult.Message == "isdbgrid")
                {
                    instanceType = MongoServerInstanceType.ShardRouter;
                }

                lock (_serverInstanceLock)
                {
                    _isMasterResult = isMasterResult;
                    _maxDocumentSize = isMasterResult.MaxBsonObjectSize;
                    _maxMessageLength = isMasterResult.MaxMessageLength;
                    _buildInfo = buildInfo;
                    this.SetState(MongoServerState.Connected,
                        instanceType,
                        isMasterResult.IsPrimary,
                        isMasterResult.IsSecondary,
                        isMasterResult.IsPassive,
                        isMasterResult.IsArbiterOnly,
                        replicaSetInformation);
                }
                ok = true;
            }
            finally
            {
                if (!ok)
                {
                    lock (_serverInstanceLock)
                    {
                        _isMasterResult = isMasterResult;
                        _maxDocumentSize = MongoDefaults.MaxDocumentSize;
                        _maxMessageLength = MongoDefaults.MaxMessageLength;
                        _buildInfo = null;
                        this.SetState(MongoServerState.Disconnected, _instanceType, false, false, false, false, null);
                    }
                }
            }
        }
开发者ID:kayone,项目名称:mongo-csharp-driver,代码行数:78,代码来源:MongoServerInstance.cs

示例7: LookupServerInformation

        // private methods
        private void LookupServerInformation(MongoConnection connection)
        {
            IsMasterResult isMasterResult = null;
            bool ok = false;
            try
            {
                var isMasterCommand = new CommandDocument("ismaster", 1);
                var tempResult = connection.RunCommand("admin", QueryFlags.SlaveOk, isMasterCommand, false);
                isMasterResult = new IsMasterResult();
                isMasterResult.Initialize(isMasterCommand, tempResult.Response);
                if (!isMasterResult.Ok)
                {
                    throw new MongoCommandException(isMasterResult);
                }

                MongoServerBuildInfo buildInfo;
                var buildInfoCommand = new CommandDocument("buildinfo", 1);
                var buildInfoResult = connection.RunCommand("admin", QueryFlags.SlaveOk, buildInfoCommand, false);
                if (buildInfoResult.Ok)
                {
                    buildInfo = MongoServerBuildInfo.FromCommandResult(buildInfoResult);
                }
                else
                {
                    // short term fix: if buildInfo fails due to auth we don't know the server version; see CSHARP-324
                    if (buildInfoResult.ErrorMessage != "need to login")
                    {
                        throw new MongoCommandException(buildInfoResult);
                    }
                    buildInfo = null;
                }

                ReplicaSetInformation replicaSetInformation = null;
                MongoServerInstanceType instanceType = MongoServerInstanceType.StandAlone;
                if (isMasterResult.ReplicaSetName != null)
                {
                    var peers = isMasterResult.Hosts.Concat(isMasterResult.Passives).Concat(isMasterResult.Arbiters).ToList();
                    replicaSetInformation = new ReplicaSetInformation(isMasterResult.ReplicaSetName, isMasterResult.Primary, peers, isMasterResult.Tags);
                    instanceType = MongoServerInstanceType.ReplicaSetMember;
                }
                else if (isMasterResult.Message != null && isMasterResult.Message == "isdbgrid")
                {
                    instanceType = MongoServerInstanceType.ShardRouter;
                }

                var newServerInfo = new ServerInformation
                {
                    BuildInfo = buildInfo,
                    InstanceType = instanceType,
                    IsArbiter = isMasterResult.IsArbiterOnly,
                    IsMasterResult = isMasterResult,
                    IsPassive = isMasterResult.IsPassive,
                    IsPrimary = isMasterResult.IsPrimary,
                    IsSecondary = isMasterResult.IsSecondary,
                    MaxDocumentSize = isMasterResult.MaxBsonObjectSize,
                    MaxMessageLength = isMasterResult.MaxMessageLength,
                    ReplicaSetInformation = replicaSetInformation
                };
                MongoServerState currentState;
                lock (_serverInstanceLock)
                {
                    currentState = _state;
                }
                SetState(currentState, newServerInfo);
                ok = true;
            }
            finally
            {
                if (!ok)
                {
                    ServerInformation currentServerInfo;
                    lock (_serverInstanceLock)
                    {
                        currentServerInfo = _serverInfo;
                    }

                    // keep the current instance type, build info, and replica set info
                    // as these aren't relevent to state and are likely still correct.
                    var newServerInfo = new ServerInformation
                    {
                        BuildInfo = currentServerInfo.BuildInfo,
                        InstanceType = currentServerInfo.InstanceType,
                        IsArbiter = false,
                        IsMasterResult = isMasterResult,
                        IsPassive = false,
                        IsPrimary = false,
                        IsSecondary = false,
                        MaxDocumentSize = currentServerInfo.MaxDocumentSize,
                        MaxMessageLength = currentServerInfo.MaxMessageLength,
                        ReplicaSetInformation = currentServerInfo.ReplicaSetInformation
                    };

                    SetState(MongoServerState.Disconnected, newServerInfo);
                }
            }
        }
开发者ID:robinNode,项目名称:mongo-csharp-driver,代码行数:97,代码来源:MongoServerInstance.cs

示例8: QueryNodeWorkItem

        // note: this method will run on a thread from the ThreadPool
        private void QueryNodeWorkItem(
            object parameters
        ) {
            // this method has to work at a very low level because the connection pool isn't set up yet
            var args = (QueryNodeParameters) parameters;
            var response = new QueryNodeResponse { Address = args.Address, EndPoint = args.EndPoint };

            try {
                var connection = new MongoConnection(null, args.EndPoint); // no connection pool
                try {
                    var isMasterCommand = new CommandDocument("ismaster", 1);
                    var isMasterResult = connection.RunCommand(server, "admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);

                    response.IsMasterResult = isMasterResult;
                    response.Connection = connection; // might become the first connection in the connection pool
                    response.IsPrimary = isMasterResult.Response["ismaster", false].ToBoolean();
                    response.MaxDocumentSize = isMasterResult.Response["maxBsonObjectSize", server.MaxDocumentSize].ToInt32();
                    response.MaxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, response.MaxDocumentSize + 1024); // derived from maxDocumentSize

                    if (server.Settings.ReplicaSetName != null) {
                        var getStatusCommand = new CommandDocument("replSetGetStatus", 1);
                        var getStatusResult = connection.RunCommand(server, "admin.$cmd", QueryFlags.SlaveOk, getStatusCommand);

                        var replicaSetName = getStatusResult.Response["set"].AsString;
                        if (replicaSetName != server.Settings.ReplicaSetName) {
                            var message = string.Format("Host {0} belongs to a different replica set: {1}", args.EndPoint, replicaSetName);
                            throw new MongoConnectionException(message);
                        }
                    }
                } catch {
                    try { connection.Close(); } catch { } // ignore exceptions
                    throw;
                }
            } catch (Exception ex) {
                response.Exception = ex;
            }

            args.ResponseQueue.Enqueue(response);
        }
开发者ID:ebix,项目名称:mongo-csharp-driver,代码行数:40,代码来源:ReplicaSetConnector.cs

示例9: VerifyState

        internal void VerifyState(
            MongoConnection connection
        )
        {
            CommandResult isMasterResult = null;
            try {
                try {
                    var isMasterCommand = new CommandDocument("ismaster", 1);
                    isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);
                } catch (MongoCommandException ex) {
                    isMasterResult = ex.CommandResult;
                    throw;
                }

                var isPrimary = isMasterResult.Response["ismaster", false].ToBoolean();
                var isSecondary = isMasterResult.Response["secondary", false].ToBoolean();
                var isPassive = isMasterResult.Response["passive", false].ToBoolean();
                var isArbiter = isMasterResult.Response["arbiterOnly", false].ToBoolean();
                // workaround for CSHARP-273
                if (isPassive && isArbiter) { isPassive = false; }

                var maxDocumentSize = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32();
                var maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize

                MongoServerBuildInfo buildInfo;
                try {
                    var buildInfoCommand = new CommandDocument("buildinfo", 1);
                    var buildInfoResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, buildInfoCommand);
                    buildInfo = new MongoServerBuildInfo(
                        buildInfoResult.Response["bits"].ToInt32(), // bits
                        buildInfoResult.Response["gitVersion"].AsString, // gitVersion
                        buildInfoResult.Response["sysInfo"].AsString, // sysInfo
                        buildInfoResult.Response["version"].AsString // versionString
                    );
                } catch (MongoCommandException ex) {
                    // short term fix: if buildInfo fails due to auth we don't know the server version
                    if (ex.CommandResult.ErrorMessage == "need to login") {
                        buildInfo = null;
                    } else {
                        throw;
                    }
                }

                this.isMasterResult = isMasterResult;
                this.maxDocumentSize = maxDocumentSize;
                this.maxMessageLength = maxMessageLength;
                this.buildInfo = buildInfo;
                this.SetState(MongoServerState.Connected, isPrimary, isSecondary, isPassive, isArbiter);
            } catch {
                this.isMasterResult = isMasterResult;
                this.maxDocumentSize = MongoDefaults.MaxDocumentSize;
                this.maxMessageLength = MongoDefaults.MaxMessageLength;
                this.buildInfo = null;
                this.SetState(MongoServerState.Disconnected, false, false, false, false);
                throw;
            }
        }
开发者ID:vshlos,项目名称:mongo-csharp-driver,代码行数:57,代码来源:MongoServerInstance.cs

示例10: Connect

        private void Connect(
            IPEndPoint endPoint,
            TimeSpan timeout
        )
        {
            var connection = new MongoConnection(null, endPoint); // no connection pool
            bool isPrimary;

            try {
                var isMasterCommand = new CommandDocument("ismaster", 1);
                var isMasterResult = connection.RunCommand(server, "admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);

                isPrimary = isMasterResult.Response["ismaster", false].ToBoolean();
                if (!isPrimary && !server.Settings.SlaveOk) {
                    throw new MongoConnectionException("Server is not a primary and SlaveOk is false");
                }

                maxDocumentSize = isMasterResult.Response["maxBsonObjectSize", server.MaxDocumentSize].ToInt32();
                maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize
            } catch {
                try { connection.Close(); } catch { } // ignore exceptions
                throw;
            }

            this.connection = connection;
            this.isPrimary = isPrimary;
        }
开发者ID:modesto,项目名称:mongo-csharp-driver,代码行数:27,代码来源:DirectConnector.cs

示例11: Connect

        private void Connect(
            MongoServerAddress address,
            TimeSpan timeout
        )
        {
            var connection = new MongoConnection(null, address); // no connection pool
            bool isPrimary;

            try {
                var isMasterCommand = new BsonDocument("ismaster", 1);
                var isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);

                isPrimary = isMasterResult["ismaster", false].ToBoolean();
                if (!isPrimary && !url.SlaveOk) {
                    throw new MongoConnectionException("Server is not a primary and SlaveOk is false");
                }
            } catch {
                try { connection.Close(); } catch { } // ignore exceptions
                throw;
            }

            this.address = address;
            this.connection = connection;
            this.isPrimary = isPrimary;
        }
开发者ID:testn,项目名称:mongo-csharp-driver,代码行数:25,代码来源:DirectConnector.cs

示例12: Ping

 internal void Ping(MongoConnection connection)
 {
     var pingCommand = new CommandDocument("ping", 1);
     connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, pingCommand, true);
 }
开发者ID:abel,项目名称:sinan,代码行数:5,代码来源:MongoServerInstance.cs

示例13: QueryNodeWorkItem

        // note: this method will run on a thread from the ThreadPool
        private void QueryNodeWorkItem(
            object parameters
        )
        {
            // this method has to work at a very low level because the connection pool isn't set up yet
            var args = (QueryNodeParameters) parameters;
            var response = new QueryNodeResponse { Address = args.Address };

            try {
                var connection = new MongoConnection(null, args.Address); // no connection pool
                try {
                    var isMasterCommand = new BsonDocument("ismaster", 1);
                    var isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);

                    response.IsMasterResult = isMasterResult;
                    response.Connection = connection; // might become the first connection in the connection pool
                    response.IsPrimary = isMasterResult["ismaster", false].ToBoolean();

                    if (url.ReplicaSetName != null) {
                        var getStatusCommand = new BsonDocument("replSetGetStatus", 1);
                        var getStatusResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, getStatusCommand);

                        var replicaSetName = getStatusResult["set"].AsString;
                        if (replicaSetName != url.ReplicaSetName) {
                            var message = string.Format("Host {0} belongs to a different replica set: {1}", args.Address, replicaSetName);
                            throw new MongoConnectionException(message);
                        }
                    }
                } catch {
                    try { connection.Close(); } catch { } // ignore exceptions
                    throw;
                }
            } catch (Exception ex) {
                response.Exception = ex;
            }

            args.ResponseQueue.Enqueue(response);
        }
开发者ID:testn,项目名称:mongo-csharp-driver,代码行数:39,代码来源:ReplicaSetConnector.cs

示例14: VerifyState

        internal void VerifyState(MongoConnection connection)
        {
            CommandResult isMasterResult = null;
            bool ok = false;
            try
            {
                var isMasterCommand = new CommandDocument("ismaster", 1);
                isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand, false);
                if (!isMasterResult.Ok)
                {
                    throw new MongoCommandException(isMasterResult);
                }

                var isPrimary = isMasterResult.Response["ismaster", false].ToBoolean();
                var isSecondary = isMasterResult.Response["secondary", false].ToBoolean();
                var isPassive = isMasterResult.Response["passive", false].ToBoolean();
                var isArbiter = isMasterResult.Response["arbiterOnly", false].ToBoolean();

                //let's suppose for now that tags are included in the isMaster command results
                var tags = isMasterResult.Response["tags", new BsonArray()].AsBsonArray.ToList();
                foreach (BsonValue tag in tags)
                    if (tag.IsString) this.tags.Add(tag.AsString);

                // workaround for CSHARP-273
                if (isPassive && isArbiter) { isPassive = false; }

                var maxDocumentSize = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32();
                var maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize

                MongoServerBuildInfo buildInfo;
                var buildInfoCommand = new CommandDocument("buildinfo", 1);
                var buildInfoResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, buildInfoCommand, false);
                if (buildInfoResult.Ok)
                {
                    buildInfo = new MongoServerBuildInfo(
                        buildInfoResult.Response["bits"].ToInt32(), // bits
                        buildInfoResult.Response["gitVersion"].AsString, // gitVersion
                        buildInfoResult.Response["sysInfo"].AsString, // sysInfo
                        buildInfoResult.Response["version"].AsString // versionString
                    );
                }
                else
                {
                    // short term fix: if buildInfo fails due to auth we don't know the server version; see CSHARP-324
                    if (buildInfoResult.ErrorMessage != "need to login")
                    {
                        throw new MongoCommandException(buildInfoResult);
                    }
                    buildInfo = null;
                }

                this.isMasterResult = isMasterResult;
                this.maxDocumentSize = maxDocumentSize;
                this.maxMessageLength = maxMessageLength;
                this.buildInfo = buildInfo;
                this.SetState(MongoServerState.Connected, isPrimary, isSecondary, isPassive, isArbiter);
                ok = true;
            }
            finally
            {
                if (!ok)
                {
                    this.isMasterResult = isMasterResult;
                    this.maxDocumentSize = MongoDefaults.MaxDocumentSize;
                    this.maxMessageLength = MongoDefaults.MaxMessageLength;
                    this.buildInfo = null;
                    this.SetState(MongoServerState.Disconnected, false, false, false, false);
                }
            }
        }
开发者ID:kamaradclimber,项目名称:mongo-csharp-driver,代码行数:70,代码来源:MongoServerInstance.cs


注:本文中的MongoConnection.RunCommand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。