MongoClientSettings.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /* Copyright 2010-2016 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using MongoDB.Bson;
  22. using MongoDB.Driver.Core.Configuration;
  23. using MongoDB.Driver.Core.Misc;
  24. using MongoDB.Shared;
  25. namespace MongoDB.Driver
  26. {
  27. /// <summary>
  28. /// The settings for a MongoDB client.
  29. /// </summary>
  30. public class MongoClientSettings : IEquatable<MongoClientSettings>, IInheritableMongoClientSettings
  31. {
  32. // private fields
  33. private string _applicationName;
  34. private Action<ClusterBuilder> _clusterConfigurator;
  35. private ConnectionMode _connectionMode;
  36. private TimeSpan _connectTimeout;
  37. private MongoCredentialStore _credentials;
  38. private GuidRepresentation _guidRepresentation;
  39. private TimeSpan _heartbeatInterval;
  40. private TimeSpan _heartbeatTimeout;
  41. private bool _ipv6;
  42. private TimeSpan _localThreshold;
  43. private TimeSpan _maxConnectionIdleTime;
  44. private TimeSpan _maxConnectionLifeTime;
  45. private int _maxConnectionPoolSize;
  46. private int _minConnectionPoolSize;
  47. private ReadConcern _readConcern;
  48. private UTF8Encoding _readEncoding;
  49. private ReadPreference _readPreference;
  50. private string _replicaSetName;
  51. private List<MongoServerAddress> _servers;
  52. private TimeSpan _serverSelectionTimeout;
  53. private TimeSpan _socketTimeout;
  54. private SslSettings _sslSettings;
  55. private bool _useSsl;
  56. private bool _verifySslCertificate;
  57. private int _waitQueueSize;
  58. private TimeSpan _waitQueueTimeout;
  59. private WriteConcern _writeConcern;
  60. private UTF8Encoding _writeEncoding;
  61. // the following fields are set when Freeze is called
  62. private bool _isFrozen;
  63. private int _frozenHashCode;
  64. private string _frozenStringRepresentation;
  65. // constructors
  66. /// <summary>
  67. /// Creates a new instance of MongoClientSettings. Usually you would use a connection string instead.
  68. /// </summary>
  69. public MongoClientSettings()
  70. {
  71. _applicationName = null;
  72. _connectionMode = ConnectionMode.Automatic;
  73. _connectTimeout = MongoDefaults.ConnectTimeout;
  74. _credentials = new MongoCredentialStore(new MongoCredential[0]);
  75. _guidRepresentation = MongoDefaults.GuidRepresentation;
  76. _heartbeatInterval = ServerSettings.DefaultHeartbeatInterval;
  77. _heartbeatTimeout = ServerSettings.DefaultHeartbeatTimeout;
  78. _ipv6 = false;
  79. _localThreshold = MongoDefaults.LocalThreshold;
  80. _maxConnectionIdleTime = MongoDefaults.MaxConnectionIdleTime;
  81. _maxConnectionLifeTime = MongoDefaults.MaxConnectionLifeTime;
  82. _maxConnectionPoolSize = MongoDefaults.MaxConnectionPoolSize;
  83. _minConnectionPoolSize = MongoDefaults.MinConnectionPoolSize;
  84. _readConcern = ReadConcern.Default;
  85. _readEncoding = null;
  86. _readPreference = ReadPreference.Primary;
  87. _replicaSetName = null;
  88. _servers = new List<MongoServerAddress> { new MongoServerAddress("localhost") };
  89. _serverSelectionTimeout = MongoDefaults.ServerSelectionTimeout;
  90. _socketTimeout = MongoDefaults.SocketTimeout;
  91. _sslSettings = null;
  92. _useSsl = false;
  93. _verifySslCertificate = true;
  94. _waitQueueSize = MongoDefaults.ComputedWaitQueueSize;
  95. _waitQueueTimeout = MongoDefaults.WaitQueueTimeout;
  96. _writeConcern = WriteConcern.Acknowledged;
  97. _writeEncoding = null;
  98. }
  99. // public properties
  100. /// <summary>
  101. /// Gets or sets the application name.
  102. /// </summary>
  103. public string ApplicationName
  104. {
  105. get { return _applicationName; }
  106. set
  107. {
  108. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  109. _applicationName = ApplicationNameHelper.EnsureApplicationNameIsValid(value, nameof(value));
  110. }
  111. }
  112. /// <summary>
  113. /// Gets or sets the cluster configurator.
  114. /// </summary>
  115. public Action<ClusterBuilder> ClusterConfigurator
  116. {
  117. get { return _clusterConfigurator; }
  118. set
  119. {
  120. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  121. _clusterConfigurator = value;
  122. }
  123. }
  124. /// <summary>
  125. /// Gets or sets the connection mode.
  126. /// </summary>
  127. public ConnectionMode ConnectionMode
  128. {
  129. get { return _connectionMode; }
  130. set
  131. {
  132. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  133. _connectionMode = value;
  134. }
  135. }
  136. /// <summary>
  137. /// Gets or sets the connect timeout.
  138. /// </summary>
  139. public TimeSpan ConnectTimeout
  140. {
  141. get { return _connectTimeout; }
  142. set
  143. {
  144. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  145. _connectTimeout = value;
  146. }
  147. }
  148. /// <summary>
  149. /// Gets or sets the credentials.
  150. /// </summary>
  151. public IEnumerable<MongoCredential> Credentials
  152. {
  153. get { return _credentials; }
  154. set
  155. {
  156. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  157. if (value == null)
  158. {
  159. throw new ArgumentNullException("value");
  160. }
  161. _credentials = new MongoCredentialStore(value);
  162. }
  163. }
  164. /// <summary>
  165. /// Gets or sets the representation to use for Guids.
  166. /// </summary>
  167. public GuidRepresentation GuidRepresentation
  168. {
  169. get { return _guidRepresentation; }
  170. set
  171. {
  172. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  173. _guidRepresentation = value;
  174. }
  175. }
  176. /// <summary>
  177. /// Gets a value indicating whether the settings have been frozen to prevent further changes.
  178. /// </summary>
  179. public bool IsFrozen
  180. {
  181. get { return _isFrozen; }
  182. }
  183. /// <summary>
  184. /// Gets or sets the heartbeat interval.
  185. /// </summary>
  186. public TimeSpan HeartbeatInterval
  187. {
  188. get { return _heartbeatInterval; }
  189. set
  190. {
  191. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  192. _heartbeatInterval = Ensure.IsGreaterThanZero(value, nameof(value));
  193. }
  194. }
  195. /// <summary>
  196. /// Gets or sets the heartbeat timeout.
  197. /// </summary>
  198. public TimeSpan HeartbeatTimeout
  199. {
  200. get { return _heartbeatTimeout; }
  201. set
  202. {
  203. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  204. _heartbeatTimeout = Ensure.IsGreaterThanZero(value, nameof(value));
  205. }
  206. }
  207. /// <summary>
  208. /// Gets or sets a value indicating whether to use IPv6.
  209. /// </summary>
  210. public bool IPv6
  211. {
  212. get { return _ipv6; }
  213. set
  214. {
  215. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  216. _ipv6 = value;
  217. }
  218. }
  219. /// <summary>
  220. /// Gets or sets the local threshold.
  221. /// </summary>
  222. public TimeSpan LocalThreshold
  223. {
  224. get { return _localThreshold; }
  225. set
  226. {
  227. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  228. _localThreshold = value;
  229. }
  230. }
  231. /// <summary>
  232. /// Gets or sets the max connection idle time.
  233. /// </summary>
  234. public TimeSpan MaxConnectionIdleTime
  235. {
  236. get { return _maxConnectionIdleTime; }
  237. set
  238. {
  239. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  240. _maxConnectionIdleTime = value;
  241. }
  242. }
  243. /// <summary>
  244. /// Gets or sets the max connection life time.
  245. /// </summary>
  246. public TimeSpan MaxConnectionLifeTime
  247. {
  248. get { return _maxConnectionLifeTime; }
  249. set
  250. {
  251. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  252. _maxConnectionLifeTime = value;
  253. }
  254. }
  255. /// <summary>
  256. /// Gets or sets the max connection pool size.
  257. /// </summary>
  258. public int MaxConnectionPoolSize
  259. {
  260. get { return _maxConnectionPoolSize; }
  261. set
  262. {
  263. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  264. _maxConnectionPoolSize = value;
  265. }
  266. }
  267. /// <summary>
  268. /// Gets or sets the min connection pool size.
  269. /// </summary>
  270. public int MinConnectionPoolSize
  271. {
  272. get { return _minConnectionPoolSize; }
  273. set
  274. {
  275. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  276. _minConnectionPoolSize = value;
  277. }
  278. }
  279. /// <summary>
  280. /// Gets or sets the read concern.
  281. /// </summary>
  282. public ReadConcern ReadConcern
  283. {
  284. get { return _readConcern; }
  285. set
  286. {
  287. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  288. _readConcern = Ensure.IsNotNull(value, nameof(value));
  289. }
  290. }
  291. /// <summary>
  292. /// Gets or sets the Read Encoding.
  293. /// </summary>
  294. public UTF8Encoding ReadEncoding
  295. {
  296. get { return _readEncoding; }
  297. set
  298. {
  299. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  300. _readEncoding = value;
  301. }
  302. }
  303. /// <summary>
  304. /// Gets or sets the read preferences.
  305. /// </summary>
  306. public ReadPreference ReadPreference
  307. {
  308. get { return _readPreference; }
  309. set
  310. {
  311. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  312. if (value == null)
  313. {
  314. throw new ArgumentNullException("value");
  315. }
  316. _readPreference = value;
  317. }
  318. }
  319. /// <summary>
  320. /// Gets or sets the name of the replica set.
  321. /// </summary>
  322. public string ReplicaSetName
  323. {
  324. get { return _replicaSetName; }
  325. set
  326. {
  327. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  328. _replicaSetName = value;
  329. }
  330. }
  331. /// <summary>
  332. /// Gets or sets the address of the server (see also Servers if using more than one address).
  333. /// </summary>
  334. public MongoServerAddress Server
  335. {
  336. get { return _servers.Single(); }
  337. set
  338. {
  339. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  340. if (value == null)
  341. {
  342. throw new ArgumentNullException("value");
  343. }
  344. _servers = new List<MongoServerAddress> { value };
  345. }
  346. }
  347. /// <summary>
  348. /// Gets or sets the list of server addresses (see also Server if using only one address).
  349. /// </summary>
  350. public IEnumerable<MongoServerAddress> Servers
  351. {
  352. get { return new ReadOnlyCollection<MongoServerAddress>(_servers); }
  353. set
  354. {
  355. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  356. if (value == null)
  357. {
  358. throw new ArgumentNullException("value");
  359. }
  360. _servers = new List<MongoServerAddress>(value);
  361. }
  362. }
  363. /// <summary>
  364. /// Gets or sets the server selection timeout.
  365. /// </summary>
  366. public TimeSpan ServerSelectionTimeout
  367. {
  368. get { return _serverSelectionTimeout; }
  369. set
  370. {
  371. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  372. _serverSelectionTimeout = value;
  373. }
  374. }
  375. /// <summary>
  376. /// Gets or sets the socket timeout.
  377. /// </summary>
  378. public TimeSpan SocketTimeout
  379. {
  380. get { return _socketTimeout; }
  381. set
  382. {
  383. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  384. _socketTimeout = value;
  385. }
  386. }
  387. /// <summary>
  388. /// Gets or sets the SSL settings.
  389. /// </summary>
  390. public SslSettings SslSettings
  391. {
  392. get { return _sslSettings; }
  393. set
  394. {
  395. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  396. _sslSettings = value;
  397. }
  398. }
  399. /// <summary>
  400. /// Gets or sets a value indicating whether to use SSL.
  401. /// </summary>
  402. public bool UseSsl
  403. {
  404. get { return _useSsl; }
  405. set
  406. {
  407. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  408. _useSsl = value;
  409. }
  410. }
  411. /// <summary>
  412. /// Gets or sets a value indicating whether to verify an SSL certificate.
  413. /// </summary>
  414. public bool VerifySslCertificate
  415. {
  416. get { return _verifySslCertificate; }
  417. set
  418. {
  419. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  420. _verifySslCertificate = value;
  421. }
  422. }
  423. /// <summary>
  424. /// Gets or sets the wait queue size.
  425. /// </summary>
  426. public int WaitQueueSize
  427. {
  428. get { return _waitQueueSize; }
  429. set
  430. {
  431. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  432. _waitQueueSize = value;
  433. }
  434. }
  435. /// <summary>
  436. /// Gets or sets the wait queue timeout.
  437. /// </summary>
  438. public TimeSpan WaitQueueTimeout
  439. {
  440. get { return _waitQueueTimeout; }
  441. set
  442. {
  443. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  444. _waitQueueTimeout = value;
  445. }
  446. }
  447. /// <summary>
  448. /// Gets or sets the WriteConcern to use.
  449. /// </summary>
  450. public WriteConcern WriteConcern
  451. {
  452. get { return _writeConcern; }
  453. set
  454. {
  455. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  456. if (value == null)
  457. {
  458. throw new ArgumentNullException("value");
  459. }
  460. _writeConcern = value;
  461. }
  462. }
  463. /// <summary>
  464. /// Gets or sets the Write Encoding.
  465. /// </summary>
  466. public UTF8Encoding WriteEncoding
  467. {
  468. get { return _writeEncoding; }
  469. set
  470. {
  471. if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
  472. _writeEncoding = value;
  473. }
  474. }
  475. // public operators
  476. /// <summary>
  477. /// Determines whether two <see cref="MongoClientSettings"/> instances are equal.
  478. /// </summary>
  479. /// <param name="lhs">The LHS.</param>
  480. /// <param name="rhs">The RHS.</param>
  481. /// <returns>
  482. /// <c>true</c> if the left hand side is equal to the right hand side; otherwise, <c>false</c>.
  483. /// </returns>
  484. public static bool operator ==(MongoClientSettings lhs, MongoClientSettings rhs)
  485. {
  486. return object.Equals(lhs, rhs); // handles lhs == null correctly
  487. }
  488. /// <summary>
  489. /// Determines whether two <see cref="MongoClientSettings"/> instances are not equal.
  490. /// </summary>
  491. /// <param name="lhs">The LHS.</param>
  492. /// <param name="rhs">The RHS.</param>
  493. /// <returns>
  494. /// <c>true</c> if the left hand side is not equal to the right hand side; otherwise, <c>false</c>.
  495. /// </returns>
  496. public static bool operator !=(MongoClientSettings lhs, MongoClientSettings rhs)
  497. {
  498. return !(lhs == rhs);
  499. }
  500. // public static methods
  501. /// <summary>
  502. /// Gets a MongoClientSettings object intialized with values from a MongoURL.
  503. /// </summary>
  504. /// <param name="url">The MongoURL.</param>
  505. /// <returns>A MongoClientSettings.</returns>
  506. public static MongoClientSettings FromUrl(MongoUrl url)
  507. {
  508. var credential = url.GetCredential();
  509. var clientSettings = new MongoClientSettings();
  510. clientSettings.ApplicationName = url.ApplicationName;
  511. clientSettings.ConnectionMode = url.ConnectionMode;
  512. clientSettings.ConnectTimeout = url.ConnectTimeout;
  513. if (credential != null)
  514. {
  515. foreach (var property in url.AuthenticationMechanismProperties)
  516. {
  517. if (property.Key.Equals("CANONICALIZE_HOST_NAME", StringComparison.OrdinalIgnoreCase))
  518. {
  519. credential = credential.WithMechanismProperty(property.Key, bool.Parse(property.Value));
  520. }
  521. else
  522. {
  523. credential = credential.WithMechanismProperty(property.Key, property.Value);
  524. }
  525. }
  526. clientSettings.Credentials = new[] { credential };
  527. }
  528. clientSettings.GuidRepresentation = url.GuidRepresentation;
  529. clientSettings.HeartbeatInterval = url.HeartbeatInterval;
  530. clientSettings.HeartbeatTimeout = url.HeartbeatTimeout;
  531. clientSettings.IPv6 = url.IPv6;
  532. clientSettings.MaxConnectionIdleTime = url.MaxConnectionIdleTime;
  533. clientSettings.MaxConnectionLifeTime = url.MaxConnectionLifeTime;
  534. clientSettings.MaxConnectionPoolSize = url.MaxConnectionPoolSize;
  535. clientSettings.MinConnectionPoolSize = url.MinConnectionPoolSize;
  536. clientSettings.ReadConcern = new ReadConcern(url.ReadConcernLevel);
  537. clientSettings.ReadEncoding = null; // ReadEncoding must be provided in code
  538. clientSettings.ReadPreference = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference;
  539. clientSettings.ReplicaSetName = url.ReplicaSetName;
  540. clientSettings.LocalThreshold = url.LocalThreshold;
  541. clientSettings.Servers = new List<MongoServerAddress>(url.Servers);
  542. clientSettings.ServerSelectionTimeout = url.ServerSelectionTimeout;
  543. clientSettings.SocketTimeout = url.SocketTimeout;
  544. clientSettings.SslSettings = null; // SSL settings must be provided in code
  545. clientSettings.UseSsl = url.UseSsl;
  546. clientSettings.VerifySslCertificate = url.VerifySslCertificate;
  547. clientSettings.WaitQueueSize = url.ComputedWaitQueueSize;
  548. clientSettings.WaitQueueTimeout = url.WaitQueueTimeout;
  549. clientSettings.WriteConcern = url.GetWriteConcern(true); // WriteConcern is enabled by default for MongoClient
  550. clientSettings.WriteEncoding = null; // WriteEncoding must be provided in code
  551. return clientSettings;
  552. }
  553. // public methods
  554. /// <summary>
  555. /// Creates a clone of the settings.
  556. /// </summary>
  557. /// <returns>A clone of the settings.</returns>
  558. public MongoClientSettings Clone()
  559. {
  560. var clone = new MongoClientSettings();
  561. clone._applicationName = _applicationName;
  562. clone._clusterConfigurator = _clusterConfigurator;
  563. clone._connectionMode = _connectionMode;
  564. clone._connectTimeout = _connectTimeout;
  565. clone._credentials = _credentials;
  566. clone._guidRepresentation = _guidRepresentation;
  567. clone._heartbeatInterval = _heartbeatInterval;
  568. clone._heartbeatTimeout = _heartbeatTimeout;
  569. clone._ipv6 = _ipv6;
  570. clone._maxConnectionIdleTime = _maxConnectionIdleTime;
  571. clone._maxConnectionLifeTime = _maxConnectionLifeTime;
  572. clone._maxConnectionPoolSize = _maxConnectionPoolSize;
  573. clone._minConnectionPoolSize = _minConnectionPoolSize;
  574. clone._readConcern = _readConcern;
  575. clone._readEncoding = _readEncoding;
  576. clone._readPreference = _readPreference;
  577. clone._replicaSetName = _replicaSetName;
  578. clone._localThreshold = _localThreshold;
  579. clone._servers = new List<MongoServerAddress>(_servers);
  580. clone._serverSelectionTimeout = _serverSelectionTimeout;
  581. clone._socketTimeout = _socketTimeout;
  582. clone._sslSettings = (_sslSettings == null) ? null : _sslSettings.Clone();
  583. clone._useSsl = _useSsl;
  584. clone._verifySslCertificate = _verifySslCertificate;
  585. clone._waitQueueSize = _waitQueueSize;
  586. clone._waitQueueTimeout = _waitQueueTimeout;
  587. clone._writeConcern = _writeConcern;
  588. clone._writeEncoding = _writeEncoding;
  589. return clone;
  590. }
  591. /// <summary>
  592. /// Determines whether the specified <see cref="MongoClientSettings" /> is equal to this instance.
  593. /// </summary>
  594. /// <param name="obj">The <see cref="MongoClientSettings" /> to compare with this instance.</param>
  595. /// <returns>
  596. /// <c>true</c> if the specified <see cref="MongoClientSettings" /> is equal to this instance; otherwise, <c>false</c>.
  597. /// </returns>
  598. public bool Equals(MongoClientSettings obj)
  599. {
  600. return Equals((object)obj); // handles obj == null correctly
  601. }
  602. /// <summary>
  603. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  604. /// </summary>
  605. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  606. /// <returns>
  607. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  608. /// </returns>
  609. public override bool Equals(object obj)
  610. {
  611. if (object.ReferenceEquals(obj, null) || GetType() != obj.GetType()) { return false; }
  612. var rhs = (MongoClientSettings)obj;
  613. return
  614. _applicationName == rhs._applicationName &&
  615. object.ReferenceEquals(_clusterConfigurator, rhs._clusterConfigurator) &&
  616. _connectionMode == rhs._connectionMode &&
  617. _connectTimeout == rhs._connectTimeout &&
  618. _credentials == rhs._credentials &&
  619. _guidRepresentation == rhs._guidRepresentation &&
  620. _heartbeatInterval == rhs._heartbeatInterval &&
  621. _heartbeatTimeout == rhs._heartbeatTimeout &&
  622. _ipv6 == rhs._ipv6 &&
  623. _maxConnectionIdleTime == rhs._maxConnectionIdleTime &&
  624. _maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
  625. _maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
  626. _minConnectionPoolSize == rhs._minConnectionPoolSize &&
  627. object.Equals(_readEncoding, rhs._readEncoding) &&
  628. object.Equals(_readConcern, rhs._readConcern) &&
  629. _readPreference == rhs._readPreference &&
  630. _replicaSetName == rhs._replicaSetName &&
  631. _localThreshold == rhs._localThreshold &&
  632. _servers.SequenceEqual(rhs._servers) &&
  633. _serverSelectionTimeout == rhs._serverSelectionTimeout &&
  634. _socketTimeout == rhs._socketTimeout &&
  635. _sslSettings == rhs._sslSettings &&
  636. _useSsl == rhs._useSsl &&
  637. _verifySslCertificate == rhs._verifySslCertificate &&
  638. _waitQueueSize == rhs._waitQueueSize &&
  639. _waitQueueTimeout == rhs._waitQueueTimeout &&
  640. _writeConcern == rhs._writeConcern &&
  641. object.Equals(_writeEncoding, rhs._writeEncoding);
  642. }
  643. /// <summary>
  644. /// Freezes the settings.
  645. /// </summary>
  646. /// <returns>The frozen settings.</returns>
  647. public MongoClientSettings Freeze()
  648. {
  649. if (!_isFrozen)
  650. {
  651. _frozenHashCode = GetHashCode();
  652. _frozenStringRepresentation = ToString();
  653. _isFrozen = true;
  654. }
  655. return this;
  656. }
  657. /// <summary>
  658. /// Returns a frozen copy of the settings.
  659. /// </summary>
  660. /// <returns>A frozen copy of the settings.</returns>
  661. public MongoClientSettings FrozenCopy()
  662. {
  663. if (_isFrozen)
  664. {
  665. return this;
  666. }
  667. else
  668. {
  669. return Clone().Freeze();
  670. }
  671. }
  672. /// <summary>
  673. /// Gets the hash code.
  674. /// </summary>
  675. /// <returns>The hash code.</returns>
  676. public override int GetHashCode()
  677. {
  678. if (_isFrozen)
  679. {
  680. return _frozenHashCode;
  681. }
  682. return new Hasher()
  683. .Hash(_applicationName)
  684. .Hash(_clusterConfigurator)
  685. .Hash(_connectionMode)
  686. .Hash(_connectTimeout)
  687. .Hash(_credentials)
  688. .Hash(_guidRepresentation)
  689. .Hash(_heartbeatInterval)
  690. .Hash(_heartbeatTimeout)
  691. .Hash(_ipv6)
  692. .Hash(_maxConnectionIdleTime)
  693. .Hash(_maxConnectionLifeTime)
  694. .Hash(_maxConnectionPoolSize)
  695. .Hash(_minConnectionPoolSize)
  696. .Hash(_readConcern)
  697. .Hash(_readEncoding)
  698. .Hash(_readPreference)
  699. .Hash(_replicaSetName)
  700. .Hash(_localThreshold)
  701. .HashElements(_servers)
  702. .Hash(_serverSelectionTimeout)
  703. .Hash(_socketTimeout)
  704. .Hash(_sslSettings)
  705. .Hash(_useSsl)
  706. .Hash(_verifySslCertificate)
  707. .Hash(_waitQueueSize)
  708. .Hash(_waitQueueTimeout)
  709. .Hash(_writeConcern)
  710. .Hash(_writeEncoding)
  711. .GetHashCode();
  712. }
  713. /// <summary>
  714. /// Returns a string representation of the settings.
  715. /// </summary>
  716. /// <returns>A string representation of the settings.</returns>
  717. public override string ToString()
  718. {
  719. if (_isFrozen)
  720. {
  721. return _frozenStringRepresentation;
  722. }
  723. var sb = new StringBuilder();
  724. if (_applicationName != null)
  725. {
  726. sb.AppendFormat("ApplicationName={0};", _applicationName);
  727. }
  728. sb.AppendFormat("ConnectionMode={0};", _connectionMode);
  729. sb.AppendFormat("ConnectTimeout={0};", _connectTimeout);
  730. sb.AppendFormat("Credentials={{{0}}};", _credentials);
  731. sb.AppendFormat("GuidRepresentation={0};", _guidRepresentation);
  732. sb.AppendFormat("HeartbeatInterval={0};", _heartbeatInterval);
  733. sb.AppendFormat("HeartbeatTimeout={0};", _heartbeatTimeout);
  734. sb.AppendFormat("IPv6={0};", _ipv6);
  735. sb.AppendFormat("MaxConnectionIdleTime={0};", _maxConnectionIdleTime);
  736. sb.AppendFormat("MaxConnectionLifeTime={0};", _maxConnectionLifeTime);
  737. sb.AppendFormat("MaxConnectionPoolSize={0};", _maxConnectionPoolSize);
  738. sb.AppendFormat("MinConnectionPoolSize={0};", _minConnectionPoolSize);
  739. if (_readEncoding != null)
  740. {
  741. sb.Append("ReadEncoding=UTF8Encoding;");
  742. }
  743. sb.AppendFormat("ReadConcern={0};", _readConcern);
  744. sb.AppendFormat("ReadPreference={0};", _readPreference);
  745. sb.AppendFormat("ReplicaSetName={0};", _replicaSetName);
  746. sb.AppendFormat("LocalThreshold={0};", _localThreshold);
  747. sb.AppendFormat("Servers={0};", string.Join(",", _servers.Select(s => s.ToString()).ToArray()));
  748. sb.AppendFormat("ServerSelectionTimeout={0};", _serverSelectionTimeout);
  749. sb.AppendFormat("SocketTimeout={0};", _socketTimeout);
  750. if (_sslSettings != null)
  751. {
  752. sb.AppendFormat("SslSettings={0};", _sslSettings);
  753. }
  754. sb.AppendFormat("Ssl={0};", _useSsl);
  755. sb.AppendFormat("SslVerifyCertificate={0};", _verifySslCertificate);
  756. sb.AppendFormat("WaitQueueSize={0};", _waitQueueSize);
  757. sb.AppendFormat("WaitQueueTimeout={0}", _waitQueueTimeout);
  758. sb.AppendFormat("WriteConcern={0};", _writeConcern);
  759. if (_writeEncoding != null)
  760. {
  761. sb.Append("WriteEncoding=UTF8Encoding;");
  762. }
  763. return sb.ToString();
  764. }
  765. // internal methods
  766. internal ClusterKey ToClusterKey()
  767. {
  768. return new ClusterKey(
  769. _applicationName,
  770. _clusterConfigurator,
  771. _connectionMode,
  772. _connectTimeout,
  773. _credentials.ToList(),
  774. _heartbeatInterval,
  775. _heartbeatTimeout,
  776. _ipv6,
  777. _localThreshold,
  778. _maxConnectionIdleTime,
  779. _maxConnectionLifeTime,
  780. _maxConnectionPoolSize,
  781. _minConnectionPoolSize,
  782. _replicaSetName,
  783. _servers.ToList(),
  784. _serverSelectionTimeout,
  785. _socketTimeout,
  786. _sslSettings,
  787. _useSsl,
  788. _verifySslCertificate,
  789. _waitQueueSize,
  790. _waitQueueTimeout);
  791. }
  792. }
  793. }