Skip to content

Commit 09028d7

Browse files
author
Emile Joubert
committed
Retain non-default binding configuration details via URI
1 parent 9b1c3c1 commit 09028d7

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,24 @@ public AuthMechanismFactory AuthMechanismFactory(string[] mechs) {
332332

333333
return null;
334334
}
335+
336+
public static explicit operator ConnectionFactory(Uri uri)
337+
{
338+
ConnectionFactory connFactory = new ConnectionFactory();
339+
if (uri.UserInfo.Length > 0)
340+
{
341+
String[] userInfo = uri.UserInfo.Split(":".ToCharArray());
342+
connFactory.UserName = Uri.UnescapeDataString(userInfo[0]);
343+
if (userInfo.Length > 1)
344+
{
345+
connFactory.Password = Uri.UnescapeDataString(userInfo[1]);
346+
}
347+
}
348+
connFactory.VirtualHost = Uri.UnescapeDataString(uri.AbsolutePath.Remove(0, 1));
349+
connFactory.Port = uri.Port;
350+
connFactory.HostName = uri.Host;
351+
return connFactory;
352+
}
353+
335354
}
336355
}

projects/examples/wcf/Test/App.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@
4343
<rabbitMQBinding>
4444
<binding
4545
name="rabbitMQConfig"
46-
broker="amqp://localhost:5672/"
46+
broker="amqp://localhost:5672/%2F"
4747
protocolversion="AMQP_0_9_1" />
4848
</rabbitMQBinding>
4949
</bindings>
5050
</system.serviceModel>
5151

5252
<appSettings>
53-
<add key="manual-test-broker-uri" value="amqp://localhost:5672/"/>
53+
<add key="manual-test-broker-uri" value="amqp://localhost:5672/%2F"/>
5454
<add key="manual-test-broker-protocol" value="AMQP_0_9_1"/>
5555
</appSettings>
5656
</configuration>

projects/examples/wcf/configDemo/Server/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<bindings>
2121
<rabbitMQBinding>
2222
<binding name="rabbitMQConfig"
23-
broker="amqp://localhost:5672/"
23+
broker="amqp://localhost:5672/%2F"
2424
protocolversion="AMQP_0_9_1"
2525
oneWay="false"/>
2626
</rabbitMQBinding>

projects/wcf/RabbitMQ.ServiceModel/src/serviceModel/RabbitMQTransportBindingElement.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public sealed class RabbitMQTransportBindingElement : TransportBindingElement
5757
{
5858
private ConnectionFactory m_connectionFactory;
5959
private IConnection m_connection;
60-
private bool m_hasBroker = false;
6160

6261
/// <summary>
6362
/// Creates a new instance of the RabbitMQTransportBindingElement Class using the default protocol.
@@ -72,12 +71,8 @@ private RabbitMQTransportBindingElement(RabbitMQTransportBindingElement other)
7271
{
7372
Broker = other.Broker;
7473
BrokerProtocol = other.BrokerProtocol;
75-
76-
m_connectionFactory.UserName = other.ConnectionFactory.UserName;
77-
m_connectionFactory.Password = other.ConnectionFactory.Password;
78-
m_connectionFactory.VirtualHost = other.ConnectionFactory.VirtualHost;
74+
m_connectionFactory = (ConnectionFactory) other.Broker;
7975
}
80-
8176

8277
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
8378
{
@@ -159,29 +154,18 @@ public override string Scheme
159154
}
160155

161156

157+
private Uri m_broker;
162158
/// <summary>
163159
/// Specifies the address of the RabbitMQ Server
164160
/// </summary>
165161
[ConfigurationProperty("broker")]
166162
public Uri Broker
167163
{
168-
get
169-
{
170-
if(!m_hasBroker) return null;
171-
UriBuilder build = new UriBuilder();
172-
build.Host = m_connectionFactory.HostName;
173-
build.Port = m_connectionFactory.Port;
174-
return build.Uri;
175-
}
164+
get { return m_broker; }
176165
set
177166
{
178-
if(value == null) m_hasBroker = false;
179-
else
180-
{
181-
m_hasBroker = true;
182-
m_connectionFactory.HostName = value.Host;
183-
m_connectionFactory.Port = value.Port;
184-
}
167+
m_broker = value;
168+
m_connectionFactory = (ConnectionFactory) value;
185169
}
186170
}
187171

0 commit comments

Comments
 (0)