@@ -41,6 +41,7 @@ public sealed class MongoClient : IMongoClient
4141 private readonly IClusterInternal _cluster ;
4242#pragma warning restore CA2213 // Disposable fields should be disposed
4343 private readonly IAutoEncryptionLibMongoCryptController _libMongoCryptController ;
44+ private readonly Func < IMongoClient , IOperationExecutor > _operationExecutorFactory ;
4445 private readonly IOperationExecutor _operationExecutor ;
4546 private readonly MongoClientSettings _settings ;
4647 private readonly ILogger < LogCategories . Client > _logger ;
@@ -61,26 +62,9 @@ public MongoClient()
6162 /// </summary>
6263 /// <param name="settings">The settings.</param>
6364 public MongoClient ( MongoClientSettings settings )
65+ : this ( client => new OperationExecutor ( client ) , settings )
6466 {
65- _settings = Ensure . IsNotNull ( settings , nameof ( settings ) ) . FrozenCopy ( ) ;
66- _logger = _settings . LoggingSettings ? . CreateLogger < LogCategories . Client > ( ) ;
6767
68- _cluster = _settings . ClusterSource . Get ( _settings . ToClusterKey ( ) ) ;
69- _operationExecutor = new OperationExecutor ( this ) ;
70- _readOperationOptions = new ( DefaultReadPreference : _settings . ReadPreference ) ;
71- _writeOperationOptions = new ( ) ;
72-
73- if ( settings . AutoEncryptionOptions != null )
74- {
75- _libMongoCryptController =
76- MongoClientSettings . Extensions . AutoEncryptionProvider . CreateAutoCryptClientController ( this , settings . AutoEncryptionOptions ) ;
77-
78- _settings . LoggingSettings ? . CreateLogger < LogCategories . Client > ( ) ? . LogTrace (
79- StructuredLogTemplateProviders . TopologyId_Message_SharedLibraryVersion ,
80- _cluster . ClusterId ,
81- "CryptClient created. Configured shared library version: " ,
82- _libMongoCryptController . CryptSharedLibraryVersion ( ) ?? "None" ) ;
83- }
8468 }
8569
8670 /// <summary>
@@ -101,10 +85,27 @@ public MongoClient(string connectionString)
10185 {
10286 }
10387
104- internal MongoClient ( IOperationExecutor operationExecutor , MongoClientSettings settings )
105- : this ( settings )
88+ internal MongoClient ( Func < IMongoClient , IOperationExecutor > operationExecutorFactory , MongoClientSettings settings )
10689 {
107- _operationExecutor = new OperationExecutorWrapper ( operationExecutor ) ;
90+ _operationExecutorFactory = Ensure . IsNotNull ( operationExecutorFactory , nameof ( operationExecutorFactory ) ) ;
91+ _settings = Ensure . IsNotNull ( settings , nameof ( settings ) ) . FrozenCopy ( ) ;
92+ _logger = _settings . LoggingSettings ? . CreateLogger < LogCategories . Client > ( ) ;
93+ _cluster = _settings . ClusterSource . Get ( _settings . ToClusterKey ( ) ) ;
94+ _operationExecutor = _operationExecutorFactory ( this ) ;
95+ _readOperationOptions = new ( DefaultReadPreference : _settings . ReadPreference ) ;
96+ _writeOperationOptions = new ( ) ;
97+
98+ if ( settings . AutoEncryptionOptions != null )
99+ {
100+ _libMongoCryptController =
101+ MongoClientSettings . Extensions . AutoEncryptionProvider . CreateAutoCryptClientController ( this , settings . AutoEncryptionOptions ) ;
102+
103+ _settings . LoggingSettings ? . CreateLogger < LogCategories . Client > ( ) ? . LogTrace (
104+ StructuredLogTemplateProviders . TopologyId_Message_SharedLibraryVersion ,
105+ _cluster . ClusterId ,
106+ "CryptClient created. Configured shared library version: " ,
107+ _libMongoCryptController . CryptSharedLibraryVersion ( ) ?? "None" ) ;
108+ }
108109 }
109110
110111 // public properties
@@ -442,7 +443,7 @@ public IMongoClient WithReadConcern(ReadConcern readConcern)
442443
443444 var newSettings = Settings . Clone ( ) ;
444445 newSettings . ReadConcern = readConcern ;
445- return new MongoClient ( _operationExecutor , newSettings ) ;
446+ return new MongoClient ( _operationExecutorFactory , newSettings ) ;
446447 }
447448
448449 /// <inheritdoc/>
@@ -454,7 +455,7 @@ public IMongoClient WithReadPreference(ReadPreference readPreference)
454455
455456 var newSettings = Settings . Clone ( ) ;
456457 newSettings . ReadPreference = readPreference ;
457- return new MongoClient ( _operationExecutor , newSettings ) ;
458+ return new MongoClient ( _operationExecutorFactory , newSettings ) ;
458459 }
459460
460461 /// <inheritdoc/>
@@ -466,7 +467,7 @@ public IMongoClient WithWriteConcern(WriteConcern writeConcern)
466467
467468 var newSettings = Settings . Clone ( ) ;
468469 newSettings . WriteConcern = writeConcern ;
469- return new MongoClient ( _operationExecutor , newSettings ) ;
470+ return new MongoClient ( _operationExecutorFactory , newSettings ) ;
470471 }
471472
472473 // private methods
0 commit comments