@@ -159,7 +159,7 @@ public ConnectionSettings Build()
159
159
}
160
160
else if ( _uris is not null )
161
161
{
162
- return new ConnectionSettings ( _uris ,
162
+ return new ConnectionSettingsViaUris ( _uris ,
163
163
_uriSelector ,
164
164
_containerId , _saslMechanism ,
165
165
_recoveryConfiguration ,
@@ -191,11 +191,8 @@ private void ValidateUris()
191
191
// </summary>
192
192
public class ConnectionSettings : IEquatable < ConnectionSettings >
193
193
{
194
- private readonly Address _address = new ( "amqp://localhost:5672" ) ;
195
- private readonly List < Uri > ? _uris ;
196
- private readonly Dictionary < Uri , Address > ? _uriToAddress ;
197
- private readonly IUriSelector _uriSelector = new RandomUriSelector ( ) ;
198
- private readonly string _virtualHost = Consts . DefaultVirtualHost ;
194
+ protected Address _address = new ( "amqp://localhost:5672" ) ;
195
+ protected string _virtualHost = Consts . DefaultVirtualHost ;
199
196
private readonly string _containerId = string . Empty ;
200
197
private readonly uint _maxFrameSize = Consts . DefaultMaxFrameSize ;
201
198
private readonly TlsSettings ? _tlsSettings ;
@@ -227,66 +224,6 @@ public ConnectionSettings(Uri uri,
227
224
}
228
225
}
229
226
230
- public ConnectionSettings ( IEnumerable < Uri > uris ,
231
- IUriSelector ? uriSelector = null ,
232
- string ? containerId = null ,
233
- SaslMechanism ? saslMechanism = null ,
234
- IRecoveryConfiguration ? recoveryConfiguration = null ,
235
- uint ? maxFrameSize = null ,
236
- TlsSettings ? tlsSettings = null )
237
- : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
238
- {
239
- _uris = uris . ToList ( ) ;
240
-
241
- if ( uriSelector is not null )
242
- {
243
- _uriSelector = uriSelector ;
244
- }
245
-
246
- _uriToAddress = new ( _uris . Count ) ;
247
-
248
- string ? tmpVirtualHost = null ;
249
-
250
- bool first = true ;
251
- foreach ( Uri uri in _uris )
252
- {
253
- ( string ? user , string ? password ) = ProcessUserInfo ( uri ) ;
254
-
255
- if ( tmpVirtualHost is null )
256
- {
257
- tmpVirtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
258
- }
259
- else
260
- {
261
- string thisVirtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
262
- if ( false == thisVirtualHost . Equals ( tmpVirtualHost , StringComparison . InvariantCultureIgnoreCase ) )
263
- {
264
- throw new ArgumentException ( $ "All AMQP URIs must use the same virtual host. Expected '{ tmpVirtualHost } ', got '{ thisVirtualHost } '") ;
265
- }
266
- }
267
-
268
- var address = new Address ( host : uri . Host ,
269
- port : uri . Port ,
270
- user : user ,
271
- password : password ,
272
- path : "/" ,
273
- scheme : uri . Scheme ) ;
274
-
275
- _uriToAddress [ uri ] = address ;
276
-
277
- if ( first )
278
- {
279
- _address = address ;
280
- first = false ;
281
- }
282
- }
283
-
284
- if ( tmpVirtualHost is not null )
285
- {
286
- _virtualHost = tmpVirtualHost ;
287
- }
288
- }
289
-
290
227
public ConnectionSettings ( string scheme ,
291
228
string host ,
292
229
int port ,
@@ -315,7 +252,7 @@ public ConnectionSettings(string scheme,
315
252
}
316
253
}
317
254
318
- private ConnectionSettings (
255
+ protected ConnectionSettings (
319
256
string ? containerId = null ,
320
257
SaslMechanism ? saslMechanism = null ,
321
258
IRecoveryConfiguration ? recoveryConfiguration = null ,
@@ -364,38 +301,9 @@ private ConnectionSettings(
364
301
public TlsSettings ? TlsSettings => _tlsSettings ;
365
302
public IRecoveryConfiguration Recovery => _recoveryConfiguration ;
366
303
367
- internal Address Address
368
- {
369
- get
370
- {
371
- if ( _uris is not null &&
372
- _uriSelector is not null &&
373
- _uriToAddress is not null )
374
- {
375
- Uri uri = _uriSelector . Select ( _uris ) ;
376
- return _uriToAddress [ uri ] ;
377
- }
378
- else
379
- {
380
- return _address ;
381
- }
382
- }
383
- }
304
+ internal virtual Address Address => _address ;
384
305
385
- internal List < Address > ? Addresses
386
- {
387
- get
388
- {
389
- if ( _uriToAddress is not null )
390
- {
391
- return _uriToAddress . Values . ToList ( ) ;
392
- }
393
- else
394
- {
395
- return null ;
396
- }
397
- }
398
- }
306
+ internal virtual IList < Address > Addresses => new [ ] { _address } ;
399
307
400
308
public override string ToString ( )
401
309
{
@@ -456,15 +364,7 @@ public override int GetHashCode()
456
364
_address . Scheme , _containerId , _address . Path ) ;
457
365
}
458
366
459
- ///<summary>
460
- /// Unescape a string, protecting '+'.
461
- /// </summary>
462
- private static string UriDecode ( string str )
463
- {
464
- return Uri . UnescapeDataString ( str . Replace ( "+" , "%2B" ) ) ;
465
- }
466
-
467
- private static ( string ? user , string ? password ) ProcessUserInfo ( Uri uri )
367
+ protected static ( string ? user , string ? password ) ProcessUserInfo ( Uri uri )
468
368
{
469
369
string ? user = null ;
470
370
string ? password = null ;
@@ -487,7 +387,7 @@ private static (string? user, string? password) ProcessUserInfo(Uri uri)
487
387
return ( user , password ) ;
488
388
}
489
389
490
- private static string ProcessUriSegmentsForVirtualHost ( Uri uri )
390
+ protected static string ProcessUriSegmentsForVirtualHost ( Uri uri )
491
391
{
492
392
// C# automatically changes URIs into a canonical form
493
393
// that has at least the path segment "/"
@@ -505,6 +405,96 @@ private static string ProcessUriSegmentsForVirtualHost(Uri uri)
505
405
return Consts . DefaultVirtualHost ;
506
406
}
507
407
}
408
+
409
+ ///<summary>
410
+ /// Unescape a string, protecting '+'.
411
+ /// </summary>
412
+ private static string UriDecode ( string str )
413
+ {
414
+ return Uri . UnescapeDataString ( str . Replace ( "+" , "%2B" ) ) ;
415
+ }
416
+ }
417
+
418
+ public class ConnectionSettingsViaUris : ConnectionSettings
419
+ {
420
+ private readonly List < Uri > _uris ;
421
+ private readonly Dictionary < Uri , Address > _uriToAddress ;
422
+ private readonly IUriSelector _uriSelector = new RandomUriSelector ( ) ;
423
+
424
+ public ConnectionSettingsViaUris ( IEnumerable < Uri > uris ,
425
+ IUriSelector ? uriSelector = null ,
426
+ string ? containerId = null ,
427
+ SaslMechanism ? saslMechanism = null ,
428
+ IRecoveryConfiguration ? recoveryConfiguration = null ,
429
+ uint ? maxFrameSize = null ,
430
+ TlsSettings ? tlsSettings = null )
431
+ : base ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
432
+ {
433
+ _uris = uris . ToList ( ) ;
434
+ if ( _uris . Count == 0 )
435
+ {
436
+ throw new ArgumentOutOfRangeException ( nameof ( uris ) , "At least one Uri is required." ) ;
437
+ }
438
+
439
+ _uriToAddress = new ( _uris . Count ) ;
440
+
441
+ if ( uriSelector is not null )
442
+ {
443
+ _uriSelector = uriSelector ;
444
+ }
445
+
446
+ string ? tmpVirtualHost = null ;
447
+
448
+ bool first = true ;
449
+ foreach ( Uri uri in _uris )
450
+ {
451
+ ( string ? user , string ? password ) = ProcessUserInfo ( uri ) ;
452
+
453
+ if ( tmpVirtualHost is null )
454
+ {
455
+ tmpVirtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
456
+ }
457
+ else
458
+ {
459
+ string thisVirtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
460
+ if ( false == thisVirtualHost . Equals ( tmpVirtualHost , StringComparison . InvariantCultureIgnoreCase ) )
461
+ {
462
+ throw new ArgumentException ( $ "All AMQP URIs must use the same virtual host. Expected '{ tmpVirtualHost } ', got '{ thisVirtualHost } '") ;
463
+ }
464
+ }
465
+
466
+ var address = new Address ( host : uri . Host ,
467
+ port : uri . Port ,
468
+ user : user ,
469
+ password : password ,
470
+ path : "/" ,
471
+ scheme : uri . Scheme ) ;
472
+
473
+ _uriToAddress [ uri ] = address ;
474
+
475
+ if ( first )
476
+ {
477
+ _address = address ;
478
+ first = false ;
479
+ }
480
+ }
481
+
482
+ if ( tmpVirtualHost is not null )
483
+ {
484
+ _virtualHost = tmpVirtualHost ;
485
+ }
486
+ }
487
+
488
+ internal override Address Address
489
+ {
490
+ get
491
+ {
492
+ Uri uri = _uriSelector . Select ( _uris ) ;
493
+ return _uriToAddress [ uri ] ;
494
+ }
495
+ }
496
+
497
+ internal override IList < Address > Addresses => _uriToAddress . Values . ToList ( ) ;
508
498
}
509
499
510
500
public class TlsSettings
0 commit comments