예제 #1
0
        /// <summary> Mongo服务器 </summary>
        /// <returns></returns>
        private MongoDatabase Server()
        {
            var server = new MongoServer(new MongoServerSettings
            {
                Servers     = Contains.Config.Mongo.Servers.Select(t => new MongoServerAddress(t.Host, t.Port)),
                Credentials =
                    Contains.Config.Mongo.Credentials.Select(
                        t => MongoCredential.CreateCredential(t.DataBase, t.User, t.Pwd))
            });

            return(server.GetDatabase(_database));
        }
예제 #2
0
        /// <summary>
        /// Gets a MongoServerSettings object intialized with values from a MongoUrl.
        /// </summary>
        /// <param name="url">The MongoUrl.</param>
        /// <returns>A MongoServerSettings.</returns>
        public static MongoServerSettings FromUrl(MongoUrl url)
        {
            var credential = MongoCredential.FromComponents(
                url.AuthenticationMechanism,
                url.AuthenticationSource ?? url.DatabaseName,
                url.Username,
                url.Password);

            var serverSettings = new MongoServerSettings();

            serverSettings.ConnectionMode = url.ConnectionMode;
            serverSettings.ConnectTimeout = url.ConnectTimeout;
            if (credential != null)
            {
                if (!string.IsNullOrEmpty(url.GssapiServiceName))
                {
                    credential = credential.WithMechanismProperty("SERVICE_NAME", url.GssapiServiceName);
                }
                serverSettings.Credentials = new[] { credential };
            }
            serverSettings.GuidRepresentation = url.GuidRepresentation;
            serverSettings.IPv6 = url.IPv6;
            serverSettings.MaxConnectionIdleTime      = url.MaxConnectionIdleTime;
            serverSettings.MaxConnectionLifeTime      = url.MaxConnectionLifeTime;
            serverSettings.MaxConnectionPoolSize      = url.MaxConnectionPoolSize;
            serverSettings.MinConnectionPoolSize      = url.MinConnectionPoolSize;
            serverSettings.ReadEncoding               = null; // ReadEncoding must be provided in code
            serverSettings.ReadPreference             = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference;
            serverSettings.ReplicaSetName             = url.ReplicaSetName;
            serverSettings.SecondaryAcceptableLatency = url.SecondaryAcceptableLatency;
            serverSettings.Servers              = new List <MongoServerAddress>(url.Servers);
            serverSettings.SocketTimeout        = url.SocketTimeout;
            serverSettings.SslSettings          = null; // SSL settings must be provided in code
            serverSettings.UseSsl               = url.UseSsl;
            serverSettings.VerifySslCertificate = url.VerifySslCertificate;
            serverSettings.WaitQueueSize        = url.ComputedWaitQueueSize;
            serverSettings.WaitQueueTimeout     = url.WaitQueueTimeout;
#pragma warning disable 618
            serverSettings.WriteConcern = url.GetWriteConcern(MongoDefaults.SafeMode.Enabled);
#pragma warning restore
            serverSettings.WriteEncoding = null; // WriteEncoding must be provided in code
            return(serverSettings);
        }
        // public static methods
        /// <summary>
        /// Gets a MongoClientSettings object intialized with values from a connection string builder.
        /// </summary>
        /// <param name="builder">The connection string builder.</param>
        /// <returns>A MongoClientSettings.</returns>
        public static MongoClientSettings FromConnectionStringBuilder(MongoConnectionStringBuilder builder)
        {
            var credential = MongoCredential.FromComponents(
                builder.AuthenticationMechanism,
                builder.AuthenticationSource ?? builder.DatabaseName,
                builder.Username,
                builder.Password);

            var clientSettings = new MongoClientSettings();

            clientSettings.ConnectionMode = builder.ConnectionMode;
            clientSettings.ConnectTimeout = builder.ConnectTimeout;
            if (credential != null)
            {
                if (!string.IsNullOrEmpty(builder.GssapiServiceName))
                {
                    credential = credential.WithMechanismProperty("SERVICE_NAME", builder.GssapiServiceName);
                }
                clientSettings.Credentials = new[] { credential };
            }
            clientSettings.GuidRepresentation = builder.GuidRepresentation;
            clientSettings.IPv6 = builder.IPv6;
            clientSettings.MaxConnectionIdleTime      = builder.MaxConnectionIdleTime;
            clientSettings.MaxConnectionLifeTime      = builder.MaxConnectionLifeTime;
            clientSettings.MaxConnectionPoolSize      = builder.MaxConnectionPoolSize;
            clientSettings.MinConnectionPoolSize      = builder.MinConnectionPoolSize;
            clientSettings.ReadEncoding               = null; // ReadEncoding must be provided in code
            clientSettings.ReadPreference             = (builder.ReadPreference == null) ? ReadPreference.Primary : builder.ReadPreference.Clone();
            clientSettings.ReplicaSetName             = builder.ReplicaSetName;
            clientSettings.SecondaryAcceptableLatency = builder.SecondaryAcceptableLatency;
            clientSettings.Servers              = new List <MongoServerAddress>(builder.Servers);
            clientSettings.SocketTimeout        = builder.SocketTimeout;
            clientSettings.SslSettings          = null; // SSL settings must be provided in code
            clientSettings.UseSsl               = builder.UseSsl;
            clientSettings.VerifySslCertificate = builder.VerifySslCertificate;
            clientSettings.WaitQueueSize        = builder.ComputedWaitQueueSize;
            clientSettings.WaitQueueTimeout     = builder.WaitQueueTimeout;
            clientSettings.WriteConcern         = builder.GetWriteConcern(true); // WriteConcern is enabled by default for MongoClient
            clientSettings.WriteEncoding        = null;                          // WriteEncoding must be provided in code
            return(clientSettings);
        }
예제 #4
0
        /// <summary>
        /// Gets a MongoServerSettings object intialized with values from a MongoConnectionStringBuilder.
        /// </summary>
        /// <param name="builder">The MongoConnectionStringBuilder.</param>
        /// <returns>A MongoServerSettings.</returns>
        public static MongoServerSettings FromConnectionStringBuilder(MongoConnectionStringBuilder builder)
        {
            var credential = MongoCredential.FromComponents(
                builder.AuthenticationMechanism,
                builder.AuthenticationSource ?? builder.DatabaseName,
                builder.Username,
                builder.Password);

            var serverSettings = new MongoServerSettings();

            serverSettings.ConnectionMode = builder.ConnectionMode;
            serverSettings.ConnectTimeout = builder.ConnectTimeout;
            if (credential != null)
            {
                serverSettings.Credentials = new[] { credential };
            }
            serverSettings.GuidRepresentation = builder.GuidRepresentation;
            serverSettings.IPv6 = builder.IPv6;
            serverSettings.MaxConnectionIdleTime      = builder.MaxConnectionIdleTime;
            serverSettings.MaxConnectionLifeTime      = builder.MaxConnectionLifeTime;
            serverSettings.MaxConnectionPoolSize      = builder.MaxConnectionPoolSize;
            serverSettings.MinConnectionPoolSize      = builder.MinConnectionPoolSize;
            serverSettings.ReadEncoding               = null; // ReadEncoding must be provided in code
            serverSettings.ReadPreference             = (builder.ReadPreference == null) ? ReadPreference.Primary : builder.ReadPreference.Clone();
            serverSettings.ReplicaSetName             = builder.ReplicaSetName;
            serverSettings.SecondaryAcceptableLatency = builder.SecondaryAcceptableLatency;
            serverSettings.Servers              = new List <MongoServerAddress>(builder.Servers);
            serverSettings.SocketTimeout        = builder.SocketTimeout;
            serverSettings.SslSettings          = null; // SSL settings must be provided in code
            serverSettings.UseSsl               = builder.UseSsl;
            serverSettings.VerifySslCertificate = builder.VerifySslCertificate;
            serverSettings.WaitQueueSize        = builder.ComputedWaitQueueSize;
            serverSettings.WaitQueueTimeout     = builder.WaitQueueTimeout;
#pragma warning disable 618
            serverSettings.WriteConcern = builder.GetWriteConcern(MongoDefaults.SafeMode.Enabled);
#pragma warning restore
            serverSettings.WriteEncoding = null; // WriteEncoding must be provided in code
            return(serverSettings);
        }
        /// <summary>
        /// Gets a MongoClientSettings object intialized with values from a MongoURL.
        /// </summary>
        /// <param name="url">The MongoURL.</param>
        /// <returns>A MongoClientSettings.</returns>
        public static MongoClientSettings FromUrl(MongoUrl url)
        {
            var credential = MongoCredential.FromComponents(
                url.AuthenticationMechanism,
                url.AuthenticationSource ?? url.DatabaseName,
                url.Username,
                url.Password);

            var clientSettings = new MongoClientSettings();

            clientSettings.ConnectionMode = url.ConnectionMode;
            clientSettings.ConnectTimeout = url.ConnectTimeout;
            if (credential != null)
            {
                clientSettings.Credentials = new[] { credential };
            }
            clientSettings.GuidRepresentation = url.GuidRepresentation;
            clientSettings.IPv6 = url.IPv6;
            clientSettings.MaxConnectionIdleTime      = url.MaxConnectionIdleTime;
            clientSettings.MaxConnectionLifeTime      = url.MaxConnectionLifeTime;
            clientSettings.MaxConnectionPoolSize      = url.MaxConnectionPoolSize;
            clientSettings.MinConnectionPoolSize      = url.MinConnectionPoolSize;
            clientSettings.ReadEncoding               = null; // ReadEncoding must be provided in code
            clientSettings.ReadPreference             = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference;
            clientSettings.ReplicaSetName             = url.ReplicaSetName;
            clientSettings.SecondaryAcceptableLatency = url.SecondaryAcceptableLatency;
            clientSettings.Servers              = new List <MongoServerAddress>(url.Servers);
            clientSettings.SocketTimeout        = url.SocketTimeout;
            clientSettings.SslSettings          = null; // SSL settings must be provided in code
            clientSettings.UseSsl               = url.UseSsl;
            clientSettings.VerifySslCertificate = url.VerifySslCertificate;
            clientSettings.WaitQueueSize        = url.ComputedWaitQueueSize;
            clientSettings.WaitQueueTimeout     = url.WaitQueueTimeout;
            clientSettings.WriteConcern         = url.GetWriteConcern(true); // WriteConcern is enabled by default for MongoClient
            clientSettings.WriteEncoding        = null;                      // WriteEncoding must be provided in code
            return(clientSettings);
        }