Skip to content

Commit dfe7b97

Browse files
Merge pull request #175 from rabbitmq/rabbitmq-dotnet-client-157
Parse ports in hostname strings passed to CreateConnection
2 parents 89eaaba + e58b811 commit dfe7b97

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,12 @@ public IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint)
445445

446446
public IFrameHandler CreateFrameHandlerForHostname(string hostname)
447447
{
448-
return CreateFrameHandler(this.Endpoint.CloneWithHostname(hostname));
448+
var ep = AmqpTcpEndpoint.Parse(hostname);
449+
ep.Ssl = this.Endpoint.Ssl;
450+
return CreateFrameHandler(ep);
449451
}
450452

453+
451454
private IFrameHandler ConfigureFrameHandler(IFrameHandler fh)
452455
{
453456
// make sure socket timeouts are higher than heartbeat

projects/client/RabbitMQ.Client/src/client/impl/AutorecoveringConnection.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,11 @@ public void Init(IList<string> hostnames)
562562
{
563563
try
564564
{
565-
fh = m_factory.CreateFrameHandler(m_factory.Endpoint.CloneWithHostname(h));
565+
var endPoint = CloneEndpointWithHostname(h);
566+
fh = m_factory.CreateFrameHandler(endPoint);
566567
reachableHostname = h;
567-
} catch (Exception caught)
568+
}
569+
catch (Exception caught)
568570
{
569571
e = caught;
570572
}
@@ -796,7 +798,8 @@ protected void RecoverConnectionDelegate()
796798
try
797799
{
798800
var nextHostname = m_factory.HostnameSelector.NextFrom(this.hostnames);
799-
var fh = m_factory.CreateFrameHandler(m_factory.Endpoint.CloneWithHostname(nextHostname));
801+
var endPoint = CloneEndpointWithHostname(nextHostname);
802+
var fh = m_factory.CreateFrameHandler(endPoint);
800803
m_delegate = new Connection(m_factory, false, fh, this.ClientProvidedName);
801804
recovering = false;
802805
}
@@ -1003,5 +1006,12 @@ protected bool ShouldTriggerConnectionRecovery(ShutdownEventArgs args)
10031006
// connectivity loss or abrupt shutdown
10041007
args.Initiator == ShutdownInitiator.Library);
10051008
}
1009+
1010+
private AmqpTcpEndpoint CloneEndpointWithHostname(string hostname)
1011+
{
1012+
var ate = AmqpTcpEndpoint.Parse(hostname);
1013+
ate.Ssl = m_factory.Endpoint.Ssl;
1014+
return ate;
1015+
}
10061016
}
10071017
}

projects/client/Unit/src/unit/TestConnectionFactory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,22 @@ public void TestProperties()
6868
Assert.AreEqual(cf.HostName, h);
6969
Assert.AreEqual(cf.Port, p);
7070
}
71+
72+
[Test]
73+
public void TestCreateConnectionParsesHostNameWithPort()
74+
{
75+
var cf = new ConnectionFactory();
76+
cf.AutomaticRecoveryEnabled = true;
77+
cf.HostName = "not_localhost";
78+
cf.Port = 1234;
79+
var conn = cf.CreateConnection(new System.Collections.Generic.List<string> { "localhost:5672" }, "oregano");
80+
conn.Close();
81+
conn.Dispose();
82+
Assert.AreEqual("not_localhost", cf.HostName);
83+
Assert.AreEqual(1234, cf.Port);
84+
Assert.AreEqual("localhost", conn.Endpoint.HostName);
85+
Assert.AreEqual(5672, conn.Endpoint.Port);
86+
}
87+
7188
}
7289
}

0 commit comments

Comments
 (0)