Skip to content

Commit 100f36e

Browse files
Travis Nickelsbording
authored andcommitted
Update transport creation with new API surface
1 parent ad38532 commit 100f36e

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

src/ServiceControl.Transports.RabbitMQ/RabbitMQConventionalRoutingTransportCustomization.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,42 @@ protected override RabbitMQTransport CreateTransport(TransportSettings transport
2727
throw new InvalidOperationException("Connection string not configured");
2828
}
2929

30+
var connectionConfiguration = ConnectionConfiguration.Create(transportSettings.ConnectionString, string.Empty);
3031
var connectionStringDictionary = ConnectionConfiguration.ParseNServiceBusConnectionString(transportSettings.ConnectionString, new StringBuilder());
31-
var disableManagementApi = GetValue(connectionStringDictionary, "DisableManagementApi", "false");
32-
if (!disableManagementApi.Equals("true", StringComparison.OrdinalIgnoreCase) && !disableManagementApi.Equals("false", StringComparison.OrdinalIgnoreCase))
32+
33+
var disableManagementApiString = GetValue(connectionStringDictionary, "DisableManagementApi", "false");
34+
if (!bool.TryParse(disableManagementApiString, out var disableManagementApi))
3335
{
3436
throw new ArgumentException("The value for 'DisableManagementApi' must be either 'true' or 'false'");
3537
}
3638

3739
var transport = new RabbitMQTransport(RoutingTopology.Conventional(queueType), transportSettings.ConnectionString, enableDelayedDelivery: false);
3840
transport.TransportTransactionMode = transport.GetSupportedTransactionModes().Contains(preferredTransactionMode) ? preferredTransactionMode : TransportTransactionMode.ReceiveOnly;
39-
transport.ManagementApiUrl = GetValue(connectionStringDictionary, "ManagementApiUrl", string.Empty);
40-
transport.UseManagementApi = disableManagementApi.Equals("false", StringComparison.OrdinalIgnoreCase);
41+
transport.UseManagementApi = !disableManagementApi;
4142

42-
rabbitMQTransport = transport;
43+
if (!transport.UseManagementApi)
44+
{
45+
rabbitMQTransport = transport;
46+
return transport;
47+
}
48+
49+
var url = GetValue(connectionStringDictionary, "ManagementApiUrl", string.Empty);
50+
var username = GetValue(connectionStringDictionary, "ManagementApiUserName", connectionConfiguration.UserName);
51+
var password = GetValue(connectionStringDictionary, "ManagementApiPassword", connectionConfiguration.Password);
4352

53+
if (!string.IsNullOrEmpty(url))
54+
{
55+
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
56+
{
57+
transport.ManagementApiConfiguration = new ManagementApiConfiguration(url, username, password);
58+
}
59+
else
60+
{
61+
transport.ManagementApiConfiguration = new ManagementApiConfiguration(url);
62+
}
63+
}
64+
65+
rabbitMQTransport = transport;
4466
return transport;
4567
}
4668

src/ServiceControl.Transports.RabbitMQ/RabbitMQDirectRoutingTransportCustomization.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,42 @@ protected override RabbitMQTransport CreateTransport(TransportSettings transport
2525
throw new InvalidOperationException("Connection string not configured");
2626
}
2727

28+
var connectionConfiguration = ConnectionConfiguration.Create(transportSettings.ConnectionString, string.Empty);
2829
var connectionStringDictionary = ConnectionConfiguration.ParseNServiceBusConnectionString(transportSettings.ConnectionString, new StringBuilder());
29-
var disableManagementApi = GetValue(connectionStringDictionary, "DisableManagementApi", "false");
30-
if (!disableManagementApi.Equals("true", StringComparison.OrdinalIgnoreCase) && !disableManagementApi.Equals("false", StringComparison.OrdinalIgnoreCase))
30+
31+
var disableManagementApiString = GetValue(connectionStringDictionary, "DisableManagementApi", "false");
32+
if (!bool.TryParse(disableManagementApiString, out var disableManagementApi))
3133
{
3234
throw new ArgumentException("The value for 'DisableManagementApi' must be either 'true' or 'false'");
3335
}
3436

3537
var transport = new RabbitMQTransport(RoutingTopology.Direct(queueType, routingKeyConvention: type => type.FullName.Replace(".", "-")), transportSettings.ConnectionString, enableDelayedDelivery: false);
3638
transport.TransportTransactionMode = transport.GetSupportedTransactionModes().Contains(preferredTransactionMode) ? preferredTransactionMode : TransportTransactionMode.ReceiveOnly;
37-
transport.ManagementApiUrl = GetValue(connectionStringDictionary, "ManagementApiUrl", string.Empty);
38-
transport.UseManagementApi = disableManagementApi.Equals("false", StringComparison.OrdinalIgnoreCase);
39+
transport.UseManagementApi = !disableManagementApi;
3940

40-
rabbitMQTransport = transport;
41+
if (!transport.UseManagementApi)
42+
{
43+
rabbitMQTransport = transport;
44+
return transport;
45+
}
46+
47+
var url = GetValue(connectionStringDictionary, "ManagementApiUrl", string.Empty);
48+
var username = GetValue(connectionStringDictionary, "ManagementApiUserName", connectionConfiguration.UserName);
49+
var password = GetValue(connectionStringDictionary, "ManagementApiPassword", connectionConfiguration.Password);
4150

51+
if (!string.IsNullOrEmpty(url))
52+
{
53+
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
54+
{
55+
transport.ManagementApiConfiguration = new ManagementApiConfiguration(url, username, password);
56+
}
57+
else
58+
{
59+
transport.ManagementApiConfiguration = new ManagementApiConfiguration(url);
60+
}
61+
}
62+
63+
rabbitMQTransport = transport;
4264
return transport;
4365
}
4466

0 commit comments

Comments
 (0)