@@ -24,6 +24,8 @@ public class ConnectionSettingsBuilder
2424 private uint _maxFrameSize = Consts . DefaultMaxFrameSize ;
2525 private SaslMechanism _saslMechanism = Client . SaslMechanism . Anonymous ;
2626 private IRecoveryConfiguration _recoveryConfiguration = new RecoveryConfiguration ( ) ;
27+ private TlsSettings ? _tlsSettings = null ;
28+ private Uri ? _uri ;
2729 private List < Uri > ? _uris ;
2830
2931 public static ConnectionSettingsBuilder Create ( )
@@ -76,10 +78,10 @@ public ConnectionSettingsBuilder VirtualHost(string virtualHost)
7678 public ConnectionSettingsBuilder MaxFrameSize ( uint maxFrameSize )
7779 {
7880 _maxFrameSize = maxFrameSize ;
79- if ( _maxFrameSize != uint . MinValue && _maxFrameSize < 512 )
81+ if ( _maxFrameSize != Consts . DefaultMaxFrameSize && _maxFrameSize < 512 )
8082 {
8183 throw new ArgumentOutOfRangeException ( nameof ( maxFrameSize ) ,
82- "maxFrameSize must be greater or equal to 512" ) ;
84+ "maxFrameSize must be 0 (no limit) or greater than or equal to 512" ) ;
8385 }
8486 return this ;
8587 }
@@ -103,21 +105,63 @@ public ConnectionSettingsBuilder RecoveryConfiguration(IRecoveryConfiguration re
103105 return this ;
104106 }
105107
108+ public ConnectionSettingsBuilder TlsSettings ( TlsSettings tlsSettings )
109+ {
110+ _tlsSettings = tlsSettings ;
111+ return this ;
112+ }
113+
114+ public ConnectionSettingsBuilder Uri ( Uri uri )
115+ {
116+ _uri = uri ;
117+ ValidateUris ( ) ;
118+ return this ;
119+ }
120+
106121 public ConnectionSettingsBuilder Uris ( IEnumerable < Uri > uris )
107122 {
108123 _uris = uris . ToList ( ) ;
124+ ValidateUris ( ) ;
109125 return this ;
110126 }
111127
112128 public ConnectionSettings Build ( )
113129 {
130+ ValidateUris ( ) ;
114131 // TODO this should do something similar to consolidate in the Java code
115- var c = new ConnectionSettings ( _scheme , _host , _port , _user ,
116- _password , _virtualHost ,
117- _containerId , _saslMechanism ,
118- _recoveryConfiguration ,
119- _maxFrameSize ) ;
120- return c ;
132+ if ( _uri is not null )
133+ {
134+ return new ConnectionSettings ( _uri ,
135+ _containerId , _saslMechanism ,
136+ _recoveryConfiguration ,
137+ _maxFrameSize ,
138+ _tlsSettings ) ;
139+ }
140+ else if ( _uris is not null )
141+ {
142+ return new ConnectionSettings ( _uris ,
143+ _containerId , _saslMechanism ,
144+ _recoveryConfiguration ,
145+ _maxFrameSize ,
146+ _tlsSettings ) ;
147+ }
148+ else
149+ {
150+ return new ConnectionSettings ( _scheme , _host , _port , _user ,
151+ _password , _virtualHost ,
152+ _containerId , _saslMechanism ,
153+ _recoveryConfiguration ,
154+ _maxFrameSize ,
155+ _tlsSettings ) ;
156+ }
157+ }
158+
159+ private void ValidateUris ( )
160+ {
161+ if ( _uri is not null && _uris is not null )
162+ {
163+ throw new ArgumentOutOfRangeException ( "uris" , "Do not set both Uri and Uris" ) ;
164+ }
121165 }
122166 }
123167
@@ -126,23 +170,22 @@ public ConnectionSettings Build()
126170 // </summary>
127171 public class ConnectionSettings : IEquatable < ConnectionSettings >
128172 {
129- private readonly Address _address ;
173+ private readonly Address _address = new ( "amqp://localhost:5672" ) ;
130174 private readonly List < Address > _addresses = new ( ) ;
131175 private readonly string _virtualHost = Consts . DefaultVirtualHost ;
132- private readonly string _containerId = "" ;
176+ private readonly string _containerId = string . Empty ;
133177 private readonly uint _maxFrameSize = Consts . DefaultMaxFrameSize ;
134178 private readonly TlsSettings ? _tlsSettings ;
135179 private readonly SaslMechanism _saslMechanism = SaslMechanism . Plain ;
136180 private readonly IRecoveryConfiguration _recoveryConfiguration = new RecoveryConfiguration ( ) ;
137181
138- /*
139- * TODO: support these:
140- SaslMechanism saslMechanism,
141- IRecoveryConfiguration recoveryConfiguration,
142- uint maxFrameSize = Consts.DefaultMaxFrameSize ,
182+ public ConnectionSettings ( Uri uri ,
183+ string ? containerId = null ,
184+ SaslMechanism ? saslMechanism = null ,
185+ IRecoveryConfiguration ? recoveryConfiguration = null ,
186+ uint ? maxFrameSize = null ,
143187 TlsSettings ? tlsSettings = null )
144- */
145- public ConnectionSettings ( Uri uri )
188+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
146189 {
147190 ( string ? user , string ? password ) = ProcessUserInfo ( uri ) ;
148191
@@ -162,14 +205,13 @@ public ConnectionSettings(Uri uri)
162205 }
163206 }
164207
165- /*
166- * TODO: support these:
167- SaslMechanism saslMechanism,
168- IRecoveryConfiguration recoveryConfiguration,
169- uint maxFrameSize = Consts.DefaultMaxFrameSize ,
208+ public ConnectionSettings ( IEnumerable < Uri > uris ,
209+ string ? containerId = null ,
210+ SaslMechanism ? saslMechanism = null ,
211+ IRecoveryConfiguration ? recoveryConfiguration = null ,
212+ uint ? maxFrameSize = null ,
170213 TlsSettings ? tlsSettings = null )
171- */
172- public ConnectionSettings ( IEnumerable < Uri > uris )
214+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
173215 {
174216 string ? tmpVirtualHost = null ;
175217
@@ -200,43 +242,75 @@ public ConnectionSettings(IEnumerable<Uri> uris)
200242 }
201243
202244 _address = _addresses [ 0 ] ;
245+
203246 if ( tmpVirtualHost is not null )
204247 {
205248 _virtualHost = tmpVirtualHost ;
206249 }
207250 }
208251
209- public ConnectionSettings ( string scheme , string host , int port ,
210- string ? user , string ? password ,
211- string virtualHost , string containerId ,
212- SaslMechanism saslMechanism ,
213- IRecoveryConfiguration recoveryConfiguration ,
214- uint maxFrameSize = Consts . DefaultMaxFrameSize ,
252+ public ConnectionSettings ( string scheme ,
253+ string host ,
254+ int port ,
255+ string ? user = null ,
256+ string ? password = null ,
257+ string ? virtualHost = null ,
258+ string containerId = "" ,
259+ SaslMechanism ? saslMechanism = null ,
260+ IRecoveryConfiguration ? recoveryConfiguration = null ,
261+ uint ? maxFrameSize = null ,
215262 TlsSettings ? tlsSettings = null )
263+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
216264 {
217265 _address = new Address ( host : host , port : port ,
218266 user : user , password : password ,
219267 path : "/" , scheme : scheme ) ;
220268 _addresses . Add ( _address ) ;
221- _containerId = containerId ;
222- _virtualHost = virtualHost ;
223- _saslMechanism = saslMechanism ;
224269
225- _maxFrameSize = maxFrameSize ;
226- if ( _maxFrameSize != uint . MinValue && _maxFrameSize < 512 )
270+ if ( virtualHost is not null )
227271 {
228- throw new ArgumentOutOfRangeException ( nameof ( maxFrameSize ) ,
229- "maxFrameSize must be greater or equal to 512" ) ;
272+ _virtualHost = virtualHost ;
230273 }
231274
232- _tlsSettings = tlsSettings ;
233-
234275 if ( _address . UseSsl && _tlsSettings == null )
235276 {
236277 _tlsSettings = new TlsSettings ( ) ;
237278 }
279+ }
280+
281+ private ConnectionSettings (
282+ string ? containerId = null ,
283+ SaslMechanism ? saslMechanism = null ,
284+ IRecoveryConfiguration ? recoveryConfiguration = null ,
285+ uint ? maxFrameSize = null ,
286+ TlsSettings ? tlsSettings = null )
287+ {
288+ if ( containerId is not null )
289+ {
290+ _containerId = containerId ;
291+ }
238292
239- _recoveryConfiguration = recoveryConfiguration ;
293+ if ( saslMechanism is not null )
294+ {
295+ _saslMechanism = saslMechanism ;
296+ }
297+
298+ if ( recoveryConfiguration is not null )
299+ {
300+ _recoveryConfiguration = recoveryConfiguration ;
301+ }
302+
303+ if ( maxFrameSize is not null )
304+ {
305+ _maxFrameSize = ( uint ) maxFrameSize ;
306+ if ( _maxFrameSize != Consts . DefaultMaxFrameSize && _maxFrameSize < 512 )
307+ {
308+ throw new ArgumentOutOfRangeException ( nameof ( maxFrameSize ) ,
309+ "maxFrameSize must be 0 (no limit) or greater than or equal to 512" ) ;
310+ }
311+ }
312+
313+ _tlsSettings = tlsSettings ;
240314 }
241315
242316 public string Host => _address . Host ;
0 commit comments