GetWriteConcern() public method

Returns a WriteConcern value based on this instance's settings and a default enabled value.
public GetWriteConcern ( bool enabledDefault ) : WriteConcern
enabledDefault bool The default enabled value.
return WriteConcern
        /// <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)
        {
            MongoCredentials defaultCredentials = null;

            if (builder.Username != null && builder.Password != null)
            {
                defaultCredentials = new MongoCredentials(builder.Username, builder.Password);
            }

            var serverSettings = new MongoServerSettings();

            serverSettings.ConnectionMode     = builder.ConnectionMode;
            serverSettings.ConnectTimeout     = builder.ConnectTimeout;
            serverSettings.CredentialsStore   = new MongoCredentialsStore();
            serverSettings.DefaultCredentials = defaultCredentials;
            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.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.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
            return(serverSettings);
        }
        /// <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)
            {
                if (!string.IsNullOrEmpty(builder.GssapiServiceName))
                {
                    credential = credential.WithMechanismProperty("SERVICE_NAME", builder.GssapiServiceName);
                }
                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);
        }
        // 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)
            {
                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);
        }
        public void TestFromMongoConnectionStringBuilder()
        {
            // set everything to non default values to test that all settings are converted
            var connectionString =
                "server=somehost;username=user1;password=password1;" +
                "connect=direct;connectTimeout=123;uuidRepresentation=pythonLegacy;ipv6=true;" +
                "maxIdleTime=124;maxLifeTime=125;maxPoolSize=126;minPoolSize=127;" +
                "readPreference=secondary;readPreferenceTags=a:1,b:2|c:3,d:4;secondaryAcceptableLatency=128;socketTimeout=129;" +
                "ssl=true;sslVerifyCertificate=false;waitqueuesize=130;waitQueueTimeout=131;" +
                "fireAndForget=false;fsync=true;journal=true;w=2;wtimeout=131";
            var builder = new MongoConnectionStringBuilder(connectionString);

            var settings = MongoClientSettings.FromConnectionStringBuilder(builder);
            Assert.AreEqual(builder.ConnectionMode, settings.ConnectionMode);
            Assert.AreEqual(builder.ConnectTimeout, settings.ConnectTimeout);
            Assert.AreEqual("{}", settings.CredentialsStore.ToString());
            Assert.AreEqual(new MongoCredentials("user1", "password1"), settings.DefaultCredentials);
            Assert.AreEqual(builder.GuidRepresentation, settings.GuidRepresentation);
            Assert.AreEqual(builder.IPv6, settings.IPv6);
            Assert.AreEqual(builder.MaxConnectionIdleTime, settings.MaxConnectionIdleTime);
            Assert.AreEqual(builder.MaxConnectionLifeTime, settings.MaxConnectionLifeTime);
            Assert.AreEqual(builder.MaxConnectionPoolSize, settings.MaxConnectionPoolSize);
            Assert.AreEqual(builder.MinConnectionPoolSize, settings.MinConnectionPoolSize);
            Assert.AreEqual(builder.ReadPreference, settings.ReadPreference);
            Assert.AreEqual(builder.ReplicaSetName, settings.ReplicaSetName);
            Assert.AreEqual(builder.SecondaryAcceptableLatency, settings.SecondaryAcceptableLatency);
            Assert.IsTrue(builder.Servers.SequenceEqual(settings.Servers));
            Assert.AreEqual(builder.SocketTimeout, settings.SocketTimeout);
            Assert.AreEqual(builder.UseSsl, settings.UseSsl);
            Assert.AreEqual(builder.VerifySslCertificate, settings.VerifySslCertificate);
            Assert.AreEqual(builder.ComputedWaitQueueSize, settings.WaitQueueSize);
            Assert.AreEqual(builder.WaitQueueTimeout, settings.WaitQueueTimeout);
            Assert.AreEqual(builder.GetWriteConcern(false), settings.WriteConcern);
        }
 public void TestGetWriteConcern_WTimeout(int? ms, string connectionString)
 {
     var wtimeout = (ms == null) ? (TimeSpan?)null : TimeSpan.FromMilliseconds(ms.Value);
     var builder = new MongoConnectionStringBuilder(connectionString);
     var writeConcern = builder.GetWriteConcern(true);
     Assert.AreEqual(wtimeout, writeConcern.WTimeout);
 }
 public void TestGetWriteConcern_W(bool enabledDefault, bool enabled, object wobj, string connectionString)
 {
     var w = (wobj == null) ? null : (wobj is int) ? (WriteConcern.WValue)new WriteConcern.WCount((int)wobj) : new WriteConcern.WMode((string)wobj);
     var builder = new MongoConnectionStringBuilder(connectionString);
     var writeConcern = builder.GetWriteConcern(enabledDefault);
     Assert.AreEqual(enabled, writeConcern.Enabled);
     Assert.AreEqual(w, writeConcern.W);
 }
 public void TestGetWriteConcern_FSync(bool? fsync, string connectionString)
 {
     var builder = new MongoConnectionStringBuilder(connectionString);
     var writeConcern = builder.GetWriteConcern(true);
     Assert.AreEqual(fsync, writeConcern.FSync);
 }
 public void TestGetWriteConcern_Enabled(bool enabledDefault, bool enabled, string connectionString)
 {
     var builder = new MongoConnectionStringBuilder(connectionString);
     var writeConcern = builder.GetWriteConcern(enabledDefault);
     Assert.AreEqual(enabled, writeConcern.Enabled);
 }
        /// <summary>
        /// Gets a MongoServerSettings object intialized with values from a MongoConnectionStringBuilder.
        /// </summary>
        /// <returns>A MongoServerSettings.</returns>
        public static MongoServerSettings FromConnectionStringBuilder(MongoConnectionStringBuilder builder)
        {
            MongoCredentials defaultCredentials = null;
            if (builder.Username != null && builder.Password != null)
            {
                defaultCredentials = new MongoCredentials(builder.Username, builder.Password);
            }

            var serverSettings = new MongoServerSettings();
            serverSettings.ConnectionMode = builder.ConnectionMode;
            serverSettings.ConnectTimeout = builder.ConnectTimeout;
            serverSettings.CredentialsStore = new MongoCredentialsStore();
            serverSettings.DefaultCredentials = defaultCredentials;
            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.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.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
            return serverSettings;
        }
        // public static methods
        /// <summary>
        /// Gets a MongoClientSettings object intialized with values from a connection string builder.
        /// </summary>
        /// <returns>A MongoClientSettings.</returns>
        public static MongoClientSettings FromConnectionStringBuilder(MongoConnectionStringBuilder builder)
        {
            MongoCredentials defaultCredentials = null;
            if (builder.Username != null && builder.Password != null)
            {
                defaultCredentials = new MongoCredentials(builder.Username, builder.Password);
            }

            var clientSettings = new MongoClientSettings();
            clientSettings.ConnectionMode = builder.ConnectionMode;
            clientSettings.ConnectTimeout = builder.ConnectTimeout;
            clientSettings.CredentialsStore = new MongoCredentialsStore();
            clientSettings.DefaultCredentials = defaultCredentials;
            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.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.UseSsl = builder.UseSsl;
            clientSettings.VerifySslCertificate = builder.VerifySslCertificate;
            clientSettings.WaitQueueSize = builder.ComputedWaitQueueSize;
            clientSettings.WaitQueueTimeout = builder.WaitQueueTimeout;
            clientSettings.WriteConcern = builder.GetWriteConcern(false); // fireAndForget default for MongoClientSettings is false
            return clientSettings;
        }
        public void TestFromMongoConnectionStringBuilder()
        {
            // set everything to non default values to test that all settings are converted
            var connectionString =
                "server=somehost;username=user1;password=password1;" +
                "connect=direct;connectTimeout=123;uuidRepresentation=pythonLegacy;ipv6=true;" +
                "maxIdleTime=124;maxLifeTime=125;maxPoolSize=126;minPoolSize=127;" +
                "readPreference=secondary;readPreferenceTags=a:1,b:2|c:3,d:4;secondaryAcceptableLatency=128;socketTimeout=129;" +
                "ssl=true;sslVerifyCertificate=false;waitqueuesize=130;waitQueueTimeout=131;" +
                "w=1;fsync=true;journal=true;w=2;wtimeout=131";
            var builder = new MongoConnectionStringBuilder(connectionString);

            var settings = MongoServerSettings.FromConnectionStringBuilder(builder);
            Assert.AreEqual(builder.ConnectionMode, settings.ConnectionMode);
            Assert.AreEqual(builder.ConnectTimeout, settings.ConnectTimeout);
            Assert.AreEqual(1, settings.Credentials.Count());
            Assert.AreEqual(builder.Username, settings.Credentials.Single().Username);
            Assert.AreEqual("admin", settings.Credentials.Single().Source);
            Assert.AreEqual(new PasswordEvidence(builder.Password), settings.Credentials.Single().Evidence);
            Assert.AreEqual(builder.GuidRepresentation, settings.GuidRepresentation);
            Assert.AreEqual(builder.IPv6, settings.IPv6);
            Assert.AreEqual(builder.MaxConnectionIdleTime, settings.MaxConnectionIdleTime);
            Assert.AreEqual(builder.MaxConnectionLifeTime, settings.MaxConnectionLifeTime);
            Assert.AreEqual(builder.MaxConnectionPoolSize, settings.MaxConnectionPoolSize);
            Assert.AreEqual(builder.MinConnectionPoolSize, settings.MinConnectionPoolSize);
            Assert.AreEqual(builder.ReadPreference, settings.ReadPreference);
            Assert.AreEqual(builder.ReplicaSetName, settings.ReplicaSetName);
            Assert.AreEqual(builder.SecondaryAcceptableLatency, settings.SecondaryAcceptableLatency);
            Assert.IsTrue(builder.Servers.SequenceEqual(settings.Servers));
            Assert.AreEqual(builder.SocketTimeout, settings.SocketTimeout);
            Assert.AreEqual(null, settings.SslSettings);
            Assert.AreEqual(builder.UseSsl, settings.UseSsl);
            Assert.AreEqual(builder.VerifySslCertificate, settings.VerifySslCertificate);
            Assert.AreEqual(builder.ComputedWaitQueueSize, settings.WaitQueueSize);
            Assert.AreEqual(builder.WaitQueueTimeout, settings.WaitQueueTimeout);
#pragma warning disable 618
            Assert.AreEqual(builder.GetWriteConcern(MongoDefaults.SafeMode.Enabled), settings.WriteConcern);
#pragma warning restore
        }
 public void TestGetWriteConcern_W(object obj, string connectionString)
 {
     var w = (obj is int) ? (WriteConcern.WValue)(int)obj : (WriteConcern.WValue)(string)obj;
     var builder = new MongoConnectionStringBuilder(connectionString);
     var writeConcern = builder.GetWriteConcern(false);
     Assert.AreEqual(w, writeConcern.W);
 }
 public void TestGetWriteConcern_FireAndForget(bool fireAndForgetDefault, bool fireAndForget, string connectionString)
 {
     var builder = new MongoConnectionStringBuilder(connectionString);
     var writeConcern = builder.GetWriteConcern(fireAndForgetDefault);
     Assert.AreEqual(fireAndForget, writeConcern.FireAndForget);
 }
        // 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 credentials = MongoCredentials.FromComponents(
                builder.AuthenticationProtocol,
                builder.AuthenticationSource,
                builder.DatabaseName,
                builder.Username,
                builder.Password);

            var clientSettings = new MongoClientSettings();
            clientSettings.ConnectionMode = builder.ConnectionMode;
            clientSettings.ConnectTimeout = builder.ConnectTimeout;
            if (credentials != null)
            {
                clientSettings.CredentialsStore.Add(credentials);
            }
            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.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.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
            return clientSettings;
        }