public static ClusterDescription Parse(BsonDocument args)
        {
            var clusterId      = new ClusterId(args.GetValue("clusterId", 1).ToInt32());
            var connectionMode = (ClusterConnectionMode)Enum.Parse(typeof(ClusterConnectionMode), args.GetValue("connectionMode", "Automatic").AsString);
            var clusterType    = (ClusterType)Enum.Parse(typeof(ClusterType), args["clusterType"].AsString);

            var numberOfServers = args["servers"].AsBsonArray.Count;
            var servers         = new List <ServerDescription>(numberOfServers);

            for (var index = 0; index < numberOfServers; index++)
            {
                var serverArgs = args["servers"].AsBsonArray[index].AsBsonDocument;
                if (!serverArgs.Contains("clusterId"))
                {
                    serverArgs["clusterId"] = clusterId.Value;
                }
                if (!serverArgs.Contains("endPoint"))
                {
                    var port = 27017 + index;
                    serverArgs["endPoint"] = $"localhost:{port}";
                }

                var server = ServerDescriptionParser.Parse(serverArgs);
                servers.Add(server);
            }

            return(new ClusterDescription(clusterId, connectionMode, clusterType, servers));
        }
Esempio n. 2
0
        public static ClusterDescription Parse(BsonDocument args)
        {
            var clusterId = new ClusterId(args.GetValue("clusterId", 1).ToInt32());

#pragma warning disable CS0618 // Type or member is obsolete
            var connectionModeSwitch = ConnectionModeSwitch.NotSet;
            var connectionMode       = ClusterConnectionMode.Automatic;
            if (args.TryGetValue("connectionMode", out var connectionModeBson))
            {
                connectionModeSwitch = ConnectionModeSwitch.UseConnectionMode;
                connectionMode       = (ClusterConnectionMode)Enum.Parse(typeof(ClusterConnectionMode), connectionModeBson.AsString);
            }

            bool?directConnection = null;
            if (args.TryGetValue("directConnection", out var directConnectionBson))
            {
                if (connectionModeSwitch == ConnectionModeSwitch.UseConnectionMode)
                {
                    throw new FormatException("connectionMode and directConnection cannot both be set.");
                }
                connectionModeSwitch = ConnectionModeSwitch.UseDirectConnection;
                directConnection     = (bool?)directConnectionBson;
            }
#pragma warning restore CS0618 // Type or member is obsolete

            var clusterType = (ClusterType)Enum.Parse(typeof(ClusterType), args["clusterType"].AsString);

            var numberOfServers = args["servers"].AsBsonArray.Count;
            var servers         = new List <ServerDescription>(numberOfServers);
            for (var index = 0; index < numberOfServers; index++)
            {
                var serverArgs = args["servers"].AsBsonArray[index].AsBsonDocument;
                if (!serverArgs.Contains("clusterId"))
                {
                    serverArgs["clusterId"] = clusterId.Value;
                }
                if (!serverArgs.Contains("endPoint"))
                {
                    var port = 27017 + index;
                    serverArgs["endPoint"] = $"localhost:{port}";
                }

                var server = ServerDescriptionParser.Parse(serverArgs);
                servers.Add(server);
            }

            return(new ClusterDescription(
                       clusterId,
                       connectionMode,
                       connectionModeSwitch,
                       directConnection,
                       dnsMonitorException: null,
                       clusterType,
                       servers));
        }