Skip to content

Commit af293d3

Browse files
committed
* Make IConnectionFactory.Uri non-nullable.
1 parent f9c9594 commit af293d3

File tree

3 files changed

+238
-136
lines changed

3 files changed

+238
-136
lines changed

projects/RabbitMQ.Client/client/api/ConnectionFactory.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
using System.Linq;
3535
using System.Net.Security;
3636
using System.Reflection;
37-
using System.Runtime.CompilerServices;
3837
using System.Security.Authentication;
3938
using System.Text;
4039
using System.Threading;
@@ -197,7 +196,6 @@ public sealed class ConnectionFactory : ConnectionFactoryBase, IConnectionFactor
197196
private TimeSpan _continuationTimeout = TimeSpan.FromSeconds(20);
198197

199198
// just here to hold the value that was set through the setter
200-
private Uri? _uri;
201199
private string? _clientProvidedName;
202200

203201
/// <summary>
@@ -360,9 +358,9 @@ public AmqpTcpEndpoint Endpoint
360358
/// <summary>
361359
/// The uri to use for the connection.
362360
/// </summary>
363-
public Uri? Uri
361+
public Uri Uri
364362
{
365-
get { return _uri; }
363+
get { return GetUri(); }
366364
set { SetUri(value); }
367365
}
368366

@@ -644,14 +642,50 @@ private IFrameHandler ConfigureFrameHandler(IFrameHandler fh)
644642
return fh;
645643
}
646644

647-
private void SetUri(Uri? uri)
645+
private Uri GetUri()
648646
{
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))
650670
{
651-
_uri = uri;
652-
return;
671+
builder.UserName = UserName;
653672
}
654673

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+
{
655689
Endpoint = new AmqpTcpEndpoint();
656690

657691
if (string.Equals("amqp", uri.Scheme, StringComparison.OrdinalIgnoreCase))
@@ -669,11 +703,13 @@ private void SetUri(Uri? uri)
669703
{
670704
throw new ArgumentException($"Wrong scheme in AMQP URI: {uri.Scheme}");
671705
}
706+
672707
string host = uri.Host;
673708
if (!string.IsNullOrEmpty(host))
674709
{
675710
HostName = host;
676711
}
712+
677713
Ssl.ServerName = HostName;
678714

679715
int port = uri.Port;
@@ -703,20 +739,19 @@ that has at least the path segment "/". */
703739
{
704740
throw new ArgumentException($"Multiple segments in path of AMQP URI: {string.Join(", ", uri.Segments)}");
705741
}
742+
706743
if (uri.Segments.Length == 2)
707744
{
708745
VirtualHost = UriDecode(uri.Segments[1]);
709746
}
710-
711-
_uri = uri;
712747
}
713748

714749
///<summary>
715750
/// Unescape a string, protecting '+'.
716751
/// </summary>
717752
private static string UriDecode(string uri)
718753
{
719-
return System.Uri.UnescapeDataString(uri.Replace("+", "%2B"));
754+
return Uri.UnescapeDataString(uri.Replace("+", "%2B"));
720755
}
721756

722757
private List<AmqpTcpEndpoint> LocalEndpoints()

projects/RabbitMQ.Client/client/api/IConnectionFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public interface IConnectionFactory
8585
/// <summary>
8686
/// Sets or gets the AMQP Uri to be used for connections.
8787
/// </summary>
88-
Uri? Uri { get; set; }
88+
Uri Uri { get; set; }
8989

9090
/// <summary>
9191
/// Default client provided name to be used for connections.

0 commit comments

Comments
 (0)