34
34
using System . Linq ;
35
35
using System . Net . Security ;
36
36
using System . Reflection ;
37
- using System . Runtime . CompilerServices ;
38
37
using System . Security . Authentication ;
39
38
using System . Text ;
40
39
using System . Threading ;
@@ -197,7 +196,6 @@ public sealed class ConnectionFactory : ConnectionFactoryBase, IConnectionFactor
197
196
private TimeSpan _continuationTimeout = TimeSpan . FromSeconds ( 20 ) ;
198
197
199
198
// just here to hold the value that was set through the setter
200
- private Uri ? _uri ;
201
199
private string ? _clientProvidedName ;
202
200
203
201
/// <summary>
@@ -360,9 +358,9 @@ public AmqpTcpEndpoint Endpoint
360
358
/// <summary>
361
359
/// The uri to use for the connection.
362
360
/// </summary>
363
- public Uri ? Uri
361
+ public Uri Uri
364
362
{
365
- get { return _uri ; }
363
+ get { return GetUri ( ) ; }
366
364
set { SetUri ( value ) ; }
367
365
}
368
366
@@ -644,14 +642,50 @@ private IFrameHandler ConfigureFrameHandler(IFrameHandler fh)
644
642
return fh ;
645
643
}
646
644
647
- private void SetUri ( Uri ? uri )
645
+ private Uri GetUri ( )
648
646
{
649
- if ( uri is null )
647
+ var builder = new UriBuilder ( ) ;
648
+
649
+ if ( Ssl . Enabled )
650
+ {
651
+ builder . Scheme = "amqps" ;
652
+ }
653
+ else
654
+ {
655
+ builder . Scheme = "amqp" ;
656
+ }
657
+
658
+ builder . Host = HostName ;
659
+
660
+ if ( Port == AmqpTcpEndpoint . UseDefaultPort )
661
+ {
662
+ builder . Port = 5672 ;
663
+ }
664
+ else
665
+ {
666
+ builder . Port = Port ;
667
+ }
668
+
669
+ if ( false == string . IsNullOrEmpty ( UserName ) )
650
670
{
651
- _uri = uri ;
652
- return ;
671
+ builder . UserName = UserName ;
653
672
}
654
673
674
+ if ( false == string . IsNullOrEmpty ( Password ) )
675
+ {
676
+ builder . Password = Password ;
677
+ }
678
+
679
+ if ( false == string . IsNullOrEmpty ( VirtualHost ) )
680
+ {
681
+ builder . Path = Uri . EscapeDataString ( VirtualHost ) ;
682
+ }
683
+
684
+ return builder . Uri ;
685
+ }
686
+
687
+ private void SetUri ( Uri uri )
688
+ {
655
689
Endpoint = new AmqpTcpEndpoint ( ) ;
656
690
657
691
if ( string . Equals ( "amqp" , uri . Scheme , StringComparison . OrdinalIgnoreCase ) )
@@ -669,11 +703,13 @@ private void SetUri(Uri? uri)
669
703
{
670
704
throw new ArgumentException ( $ "Wrong scheme in AMQP URI: { uri . Scheme } ") ;
671
705
}
706
+
672
707
string host = uri . Host ;
673
708
if ( ! string . IsNullOrEmpty ( host ) )
674
709
{
675
710
HostName = host ;
676
711
}
712
+
677
713
Ssl . ServerName = HostName ;
678
714
679
715
int port = uri . Port ;
@@ -703,20 +739,19 @@ that has at least the path segment "/". */
703
739
{
704
740
throw new ArgumentException ( $ "Multiple segments in path of AMQP URI: { string . Join ( ", " , uri . Segments ) } ") ;
705
741
}
742
+
706
743
if ( uri . Segments . Length == 2 )
707
744
{
708
745
VirtualHost = UriDecode ( uri . Segments [ 1 ] ) ;
709
746
}
710
-
711
- _uri = uri ;
712
747
}
713
748
714
749
///<summary>
715
750
/// Unescape a string, protecting '+'.
716
751
/// </summary>
717
752
private static string UriDecode ( string uri )
718
753
{
719
- return System . Uri . UnescapeDataString ( uri . Replace ( "+" , "%2B" ) ) ;
754
+ return Uri . UnescapeDataString ( uri . Replace ( "+" , "%2B" ) ) ;
720
755
}
721
756
722
757
private List < AmqpTcpEndpoint > LocalEndpoints ( )
0 commit comments