@@ -59,6 +59,10 @@ public class MongoClientSettings : IEquatable<MongoClientSettings>, IInheritable
59
59
private TimeSpan _maxConnectionLifeTime ;
60
60
private int _maxConnectionPoolSize ;
61
61
private int _minConnectionPoolSize ;
62
+ private string _proxyHost ;
63
+ private int ? _proxyPort ;
64
+ private string _proxyUsername ;
65
+ private string _proxyPassword ;
62
66
private ReadConcern _readConcern ;
63
67
private UTF8Encoding _readEncoding ;
64
68
private ReadPreference _readPreference ;
@@ -110,6 +114,10 @@ public MongoClientSettings()
110
114
_maxConnectionLifeTime = MongoDefaults . MaxConnectionLifeTime ;
111
115
_maxConnectionPoolSize = MongoDefaults . MaxConnectionPoolSize ;
112
116
_minConnectionPoolSize = MongoDefaults . MinConnectionPoolSize ;
117
+ _proxyHost = null ;
118
+ _proxyPort = null ;
119
+ _proxyUsername = null ;
120
+ _proxyPassword = null ;
113
121
_readConcern = ReadConcern . Default ;
114
122
_readEncoding = null ;
115
123
_readPreference = ReadPreference . Primary ;
@@ -428,6 +436,64 @@ public int MinConnectionPoolSize
428
436
}
429
437
}
430
438
439
+ /// <summary>
440
+ /// Gets or sets the proxy host.
441
+ /// </summary>
442
+ public string ProxyHost
443
+ {
444
+ get => _proxyHost ;
445
+ set
446
+ {
447
+ if ( _isFrozen ) { throw new InvalidOperationException ( "MongoClientSettings is frozen." ) ; }
448
+ _proxyHost = Ensure . IsNotNullOrEmpty ( value , nameof ( ProxyHost ) ) ;
449
+ }
450
+ }
451
+
452
+ /// <summary>
453
+ /// Gets or sets the proxy port.
454
+ /// </summary>
455
+ public int ? ProxyPort
456
+ {
457
+ get => _proxyPort ;
458
+ set
459
+ {
460
+ if ( _isFrozen ) { throw new InvalidOperationException ( "MongoClientSettings is frozen." ) ; }
461
+
462
+ if ( value is < 0 or > 65535 )
463
+ {
464
+ throw new MongoConfigurationException ( "ProxyPort must be between 0 and 65535." ) ;
465
+ }
466
+
467
+ _proxyPort = value ;
468
+ }
469
+ }
470
+
471
+ /// <summary>
472
+ /// Gets or sets the proxy username.
473
+ /// </summary>
474
+ public string ProxyUsername
475
+ {
476
+ get => _proxyUsername ;
477
+ set
478
+ {
479
+ if ( _isFrozen ) { throw new InvalidOperationException ( "MongoClientSettings is frozen." ) ; }
480
+ _proxyUsername = Ensure . IsNotNullOrEmpty ( value , nameof ( ProxyUsername ) ) ;
481
+ }
482
+ }
483
+
484
+ /// <summary>
485
+ /// Gets or sets the proxy password.
486
+ /// </summary>
487
+ public string ProxyPassword
488
+ {
489
+ get => _proxyPassword ;
490
+ set
491
+ {
492
+ if ( _isFrozen ) { throw new InvalidOperationException ( "MongoClientSettings is frozen." ) ; }
493
+ _proxyPassword = Ensure . IsNotNullOrEmpty ( value , nameof ( ProxyPassword ) ) ;
494
+ }
495
+ }
496
+
431
497
/// <summary>
432
498
/// Gets or sets the read concern.
433
499
/// </summary>
@@ -863,6 +929,10 @@ public static MongoClientSettings FromUrl(MongoUrl url)
863
929
clientSettings . MaxConnectionLifeTime = url . MaxConnectionLifeTime ;
864
930
clientSettings . MaxConnectionPoolSize = ConnectionStringConversions . GetEffectiveMaxConnections ( url . MaxConnectionPoolSize ) ;
865
931
clientSettings . MinConnectionPoolSize = url . MinConnectionPoolSize ;
932
+ clientSettings . ProxyHost = url . ProxyHost ;
933
+ clientSettings . ProxyPort = url . ProxyPort ;
934
+ clientSettings . ProxyUsername = url . ProxyUsername ;
935
+ clientSettings . ProxyPassword = url . ProxyPassword ;
866
936
clientSettings . ReadConcern = new ReadConcern ( url . ReadConcernLevel ) ;
867
937
clientSettings . ReadEncoding = null ; // ReadEncoding must be provided in code
868
938
clientSettings . ReadPreference = ( url . ReadPreference == null ) ? ReadPreference . Primary : url . ReadPreference ;
@@ -920,6 +990,10 @@ public MongoClientSettings Clone()
920
990
clone . _maxConnectionLifeTime = _maxConnectionLifeTime ;
921
991
clone . _maxConnectionPoolSize = _maxConnectionPoolSize ;
922
992
clone . _minConnectionPoolSize = _minConnectionPoolSize ;
993
+ clone . _proxyHost = _proxyHost ;
994
+ clone . _proxyPort = _proxyPort ;
995
+ clone . _proxyUsername = _proxyUsername ;
996
+ clone . _proxyPassword = _proxyPassword ;
923
997
clone . _readConcern = _readConcern ;
924
998
clone . _readEncoding = _readEncoding ;
925
999
clone . _readPreference = _readPreference ;
@@ -989,6 +1063,10 @@ public override bool Equals(object obj)
989
1063
_maxConnectionLifeTime == rhs . _maxConnectionLifeTime &&
990
1064
_maxConnectionPoolSize == rhs . _maxConnectionPoolSize &&
991
1065
_minConnectionPoolSize == rhs . _minConnectionPoolSize &&
1066
+ _proxyHost == rhs . _proxyHost &&
1067
+ _proxyPort == rhs . _proxyPort &&
1068
+ _proxyUsername == rhs . _proxyUsername &&
1069
+ _proxyPassword == rhs . _proxyPassword &&
992
1070
object . Equals ( _readEncoding , rhs . _readEncoding ) &&
993
1071
object . Equals ( _readConcern , rhs . _readConcern ) &&
994
1072
object . Equals ( _readPreference , rhs . _readPreference ) &&
@@ -1076,6 +1154,10 @@ public override int GetHashCode()
1076
1154
. Hash ( _maxConnectionLifeTime )
1077
1155
. Hash ( _maxConnectionPoolSize )
1078
1156
. Hash ( _minConnectionPoolSize )
1157
+ . Hash ( _proxyHost )
1158
+ . Hash ( _proxyPort )
1159
+ . Hash ( _proxyUsername )
1160
+ . Hash ( _proxyPassword )
1079
1161
. Hash ( _readConcern )
1080
1162
. Hash ( _readEncoding )
1081
1163
. Hash ( _readPreference )
@@ -1145,6 +1227,22 @@ public override string ToString()
1145
1227
sb . AppendFormat ( "MaxConnectionLifeTime={0};" , _maxConnectionLifeTime ) ;
1146
1228
sb . AppendFormat ( "MaxConnectionPoolSize={0};" , _maxConnectionPoolSize ) ;
1147
1229
sb . AppendFormat ( "MinConnectionPoolSize={0};" , _minConnectionPoolSize ) ;
1230
+ if ( _proxyHost != null )
1231
+ {
1232
+ sb . AppendFormat ( "ProxyHost={0};" , _proxyHost ) ;
1233
+ }
1234
+ if ( _proxyPort != null )
1235
+ {
1236
+ sb . AppendFormat ( "ProxyPort={0};" , _proxyPort . Value ) ;
1237
+ }
1238
+ if ( _proxyUsername != null )
1239
+ {
1240
+ sb . AppendFormat ( "ProxyUsername={0};" , _proxyUsername ) ;
1241
+ }
1242
+ if ( _proxyPassword != null )
1243
+ {
1244
+ sb . AppendFormat ( "ProxyPassword={0};" , _proxyPassword ) ;
1245
+ }
1148
1246
if ( _readEncoding != null )
1149
1247
{
1150
1248
sb . Append ( "ReadEncoding=UTF8Encoding;" ) ;
@@ -1297,6 +1395,29 @@ private void ThrowIfSettingsAreInvalid()
1297
1395
throw new InvalidOperationException ( "Load balanced mode cannot be used with direct connection." ) ;
1298
1396
}
1299
1397
}
1398
+
1399
+ if ( string . IsNullOrEmpty ( _proxyHost ) )
1400
+ {
1401
+ if ( _proxyPort is not null )
1402
+ {
1403
+ throw new InvalidOperationException ( "ProxyPort cannot be specified without ProxyHost." ) ;
1404
+ }
1405
+
1406
+ if ( ! string . IsNullOrEmpty ( _proxyUsername ) )
1407
+ {
1408
+ throw new InvalidOperationException ( "ProxyUsername cannot be specified without ProxyHost." ) ;
1409
+ }
1410
+
1411
+ if ( ! string . IsNullOrEmpty ( _proxyPassword ) )
1412
+ {
1413
+ throw new InvalidOperationException ( "ProxyPassword cannot be specified without ProxyHost." ) ;
1414
+ }
1415
+ }
1416
+
1417
+ if ( string . IsNullOrEmpty ( _proxyUsername ) != string . IsNullOrEmpty ( _proxyPassword ) )
1418
+ {
1419
+ throw new InvalidOperationException ( "ProxyUsername and ProxyPassword must both be specified or neither." ) ;
1420
+ }
1300
1421
}
1301
1422
}
1302
1423
}
0 commit comments