Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.

Commit ee9442d

Browse files
author
Anton Vorontsov
committed
Added SslOption for TcpEndpoint.
1 parent df383a5 commit ee9442d

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace RabbitMQ.Client.Core.DependencyInjection.Configuration
2+
{
3+
/// <summary>
4+
/// Ssl option model.
5+
/// </summary>
6+
public class RabbitMqSslOption
7+
{
8+
/// <summary>
9+
/// Canonical server name.
10+
/// </summary>
11+
public string ServerName { get; set; }
12+
13+
/// <summary>
14+
/// Path to the certificate.
15+
/// </summary>
16+
public string CertificatePath { get; set; }
17+
18+
/// <summary>
19+
/// Flag that defines if certificate should be used.
20+
/// </summary>
21+
public bool Enabled { get; set; } = true;
22+
}
23+
}

src/RabbitMQ.Client.Core.DependencyInjection/Configuration/RabbitMqTcpEndpoint.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@ public class RabbitMqTcpEndpoint
1414
/// Tcp connection port.
1515
/// </summary>
1616
public int Port { get; set; } = 5672;
17+
18+
/// <summary>
19+
/// Ssl option.
20+
/// </summary>
21+
/// <remarks>
22+
/// Should be null if certificate should not be used.
23+
/// </remarks>
24+
public RabbitMqSslOption SslOption { get; set; }
1725
}
1826
}

src/RabbitMQ.Client.Core.DependencyInjection/Services/RabbitMqConnectionFactory.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading;
45
using RabbitMQ.Client.Core.DependencyInjection.Configuration;
@@ -41,8 +42,7 @@ public IConnection CreateRabbitMqConnection(RabbitMqClientOptions options)
4142

4243
if (options.TcpEndpoints?.Any() == true)
4344
{
44-
var clientEndpoints = options.TcpEndpoints.Select(x => new AmqpTcpEndpoint(x.HostName, x.Port)).ToList();
45-
return TryToCreateConnection(() => factory.CreateConnection(clientEndpoints), options.InitialConnectionRetries, options.InitialConnectionRetryTimeoutMilliseconds);
45+
return CreateConnectionWithTcpEndpoints(options, factory);
4646
}
4747

4848
return string.IsNullOrEmpty(options.ClientProvidedName)
@@ -57,6 +57,25 @@ public IConnection CreateRabbitMqConnection(RabbitMqClientOptions options)
5757
/// <returns>A consumer instance <see cref="AsyncEventingBasicConsumer"/>.</returns>
5858
public AsyncEventingBasicConsumer CreateConsumer(IModel channel) => new AsyncEventingBasicConsumer(channel);
5959

60+
static IConnection CreateConnectionWithTcpEndpoints(RabbitMqClientOptions options, ConnectionFactory factory)
61+
{
62+
var clientEndpoints = new List<AmqpTcpEndpoint>();
63+
foreach (var endpoint in options.TcpEndpoints)
64+
{
65+
var sslOption = endpoint.SslOption;
66+
if (sslOption != null)
67+
{
68+
var convertedOption = new SslOption(sslOption.ServerName, sslOption.CertificatePath, sslOption.Enabled);
69+
clientEndpoints.Add(new AmqpTcpEndpoint(endpoint.HostName, endpoint.Port, convertedOption));
70+
}
71+
else
72+
{
73+
clientEndpoints.Add(new AmqpTcpEndpoint(endpoint.HostName, endpoint.Port));
74+
}
75+
}
76+
return TryToCreateConnection(() => factory.CreateConnection(clientEndpoints), options.InitialConnectionRetries, options.InitialConnectionRetryTimeoutMilliseconds);
77+
}
78+
6079
static IConnection CreateNamedConnection(RabbitMqClientOptions options, ConnectionFactory factory)
6180
{
6281
if (options.HostNames?.Any() == true)

0 commit comments

Comments
 (0)