MongoDB.Driver.MongoServerBuildInfo.FromCommandResult C# (CSharp) Méthode

FromCommandResult() public static méthode

Creates a new instance of MongoServerBuildInfo initialized from the result of a buildinfo command.
public static FromCommandResult ( CommandResult result ) : MongoServerBuildInfo
result CommandResult A CommandResult.
Résultat MongoServerBuildInfo
        public static MongoServerBuildInfo FromCommandResult(CommandResult result)
        {
            var document = result.Response;
            return new MongoServerBuildInfo(
                document["bits"].ToInt32(),
                document["gitVersion"].AsString,
                document["sysInfo"].AsString,
                document["version"].AsString
            );
        }
    }

Usage Example

        // 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);
                }
            }
        }
All Usage Examples Of MongoDB.Driver.MongoServerBuildInfo::FromCommandResult