/// <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) { if (!url.IsResolved) { var connectionMode = url.ConnectionMode; var resolveHosts = connectionMode == ConnectionMode.Direct || connectionMode == ConnectionMode.Standalone; url = url.Resolve(resolveHosts); } var credential = url.GetCredential(); var clientSettings = new MongoClientSettings(); clientSettings.AllowInsecureTls = url.AllowInsecureTls; clientSettings.ApplicationName = url.ApplicationName; clientSettings.Compressors = url.Compressors; clientSettings.ConnectionMode = url.ConnectionMode; clientSettings.ConnectTimeout = url.ConnectTimeout; if (credential != null) { foreach (var property in url.AuthenticationMechanismProperties) { if (property.Key.Equals("CANONICALIZE_HOST_NAME", StringComparison.OrdinalIgnoreCase)) { credential = credential.WithMechanismProperty(property.Key, bool.Parse(property.Value)); } else { credential = credential.WithMechanismProperty(property.Key, property.Value); } } clientSettings.Credential = credential; } clientSettings.GuidRepresentation = url.GuidRepresentation; clientSettings.HeartbeatInterval = url.HeartbeatInterval; clientSettings.HeartbeatTimeout = url.HeartbeatTimeout; clientSettings.IPv6 = url.IPv6; clientSettings.MaxConnectionIdleTime = url.MaxConnectionIdleTime; clientSettings.MaxConnectionLifeTime = url.MaxConnectionLifeTime; clientSettings.MaxConnectionPoolSize = url.MaxConnectionPoolSize; clientSettings.MinConnectionPoolSize = url.MinConnectionPoolSize; clientSettings.ReadConcern = new ReadConcern(url.ReadConcernLevel); clientSettings.ReadEncoding = null; // ReadEncoding must be provided in code clientSettings.ReadPreference = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference; clientSettings.ReplicaSetName = url.ReplicaSetName; clientSettings.RetryReads = url.RetryReads.GetValueOrDefault(true); clientSettings.RetryWrites = url.RetryWrites.GetValueOrDefault(true); clientSettings.LocalThreshold = url.LocalThreshold; clientSettings.Scheme = url.Scheme; clientSettings.Servers = new List <MongoServerAddress>(url.Servers); clientSettings.ServerSelectionTimeout = url.ServerSelectionTimeout; clientSettings.SocketTimeout = url.SocketTimeout; clientSettings.SslSettings = null; // SSL settings must be provided in code clientSettings.UseTls = url.UseTls; 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); }
/// <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) { if (!url.IsResolved) { bool resolveHosts; #pragma warning disable CS0618 // Type or member is obsolete if (url.ConnectionModeSwitch == ConnectionModeSwitch.UseDirectConnection) #pragma warning restore CS0618 // Type or member is obsolete { resolveHosts = url.DirectConnection.GetValueOrDefault(); } else { #pragma warning disable CS0618 // Type or member is obsolete var connectionMode = url.ConnectionMode; #pragma warning restore CS0618 // Type or member is obsolete resolveHosts = connectionMode == ConnectionMode.Direct || connectionMode == ConnectionMode.Standalone; } url = url.Resolve(resolveHosts); } var credential = url.GetCredential(); var clientSettings = new MongoClientSettings(); clientSettings.AllowInsecureTls = url.AllowInsecureTls; clientSettings.ApplicationName = url.ApplicationName; clientSettings.AutoEncryptionOptions = null; // must be configured via code clientSettings.Compressors = url.Compressors; #pragma warning disable CS0618 // Type or member is obsolete if (url.ConnectionModeSwitch == ConnectionModeSwitch.UseConnectionMode) { clientSettings.ConnectionMode = url.ConnectionMode; #pragma warning restore CS0618 // Type or member is obsolete } clientSettings.ConnectTimeout = url.ConnectTimeout; if (credential != null) { foreach (var property in url.AuthenticationMechanismProperties) { if (property.Key.Equals("CANONICALIZE_HOST_NAME", StringComparison.OrdinalIgnoreCase)) { credential = credential.WithMechanismProperty(property.Key, bool.Parse(property.Value)); } else { credential = credential.WithMechanismProperty(property.Key, property.Value); } } clientSettings.Credential = credential; } #pragma warning disable CS0618 // Type or member is obsolete if (url.ConnectionModeSwitch == ConnectionModeSwitch.UseDirectConnection) #pragma warning restore CS0618 // Type or member is obsolete { clientSettings.DirectConnection = url.DirectConnection; } #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { clientSettings.GuidRepresentation = url.GuidRepresentation; } #pragma warning restore 618 clientSettings.HeartbeatInterval = url.HeartbeatInterval; clientSettings.HeartbeatTimeout = url.HeartbeatTimeout; clientSettings.IPv6 = url.IPv6; clientSettings.MaxConnectionIdleTime = url.MaxConnectionIdleTime; clientSettings.MaxConnectionLifeTime = url.MaxConnectionLifeTime; clientSettings.MaxConnectionPoolSize = url.MaxConnectionPoolSize; clientSettings.MinConnectionPoolSize = url.MinConnectionPoolSize; clientSettings.ReadConcern = new ReadConcern(url.ReadConcernLevel); clientSettings.ReadEncoding = null; // ReadEncoding must be provided in code clientSettings.ReadPreference = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference; clientSettings.ReplicaSetName = url.ReplicaSetName; clientSettings.RetryReads = url.RetryReads.GetValueOrDefault(true); clientSettings.RetryWrites = url.RetryWrites.GetValueOrDefault(true); clientSettings.LocalThreshold = url.LocalThreshold; clientSettings.Scheme = url.Scheme; clientSettings.Servers = new List<MongoServerAddress>(url.Servers); clientSettings.ServerSelectionTimeout = url.ServerSelectionTimeout; clientSettings.SocketTimeout = url.SocketTimeout; clientSettings.SslSettings = null; if (url.TlsDisableCertificateRevocationCheck) { clientSettings.SslSettings = new SslSettings { CheckCertificateRevocation = false }; } clientSettings.UseTls = url.UseTls; #pragma warning disable 618 clientSettings.WaitQueueSize = url.ComputedWaitQueueSize; #pragma warning restore 618 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; }