From 3ae40b9c376783393df9d38efee2013c515a946f Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 18 Jul 2026 14:19:20 +0800 Subject: [PATCH] Preserve RabbitMQ connection settings from configuration --- .../Volo/Abp/RabbitMQ/AbpRabbitMqModule.cs | 17 ++- .../Abp/RabbitMQ/AbpRabbitMqOptions_Tests.cs | 129 ++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 framework/test/Volo.Abp.RabbitMQ.Tests/Volo/Abp/RabbitMQ/AbpRabbitMqOptions_Tests.cs diff --git a/framework/src/Volo.Abp.RabbitMQ/Volo/Abp/RabbitMQ/AbpRabbitMqModule.cs b/framework/src/Volo.Abp.RabbitMQ/Volo/Abp/RabbitMQ/AbpRabbitMqModule.cs index 74b41dea23..e1da7d4036 100644 --- a/framework/src/Volo.Abp.RabbitMQ/Volo/Abp/RabbitMQ/AbpRabbitMqModule.cs +++ b/framework/src/Volo.Abp.RabbitMQ/Volo/Abp/RabbitMQ/AbpRabbitMqModule.cs @@ -1,5 +1,7 @@ using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using RabbitMQ.Client; using Volo.Abp.Json; using Volo.Abp.Modularity; using Volo.Abp.Threading; @@ -18,9 +20,20 @@ public class AbpRabbitMqModule : AbpModule Configure(configuration.GetSection("RabbitMQ")); Configure(options => { - foreach (var connectionFactory in options.Connections.Values) + var connectionsSection = configuration.GetSection("RabbitMQ:Connections"); + foreach (var connection in options.Connections) { - connectionFactory.AutomaticRecoveryEnabled = false; + var connectionSection = connectionsSection.GetSection(connection.Key); + connectionSection.GetSection(nameof(ConnectionFactory.Ssl)).Bind(connection.Value.Ssl); + + var maxInboundMessageBodySize = connectionSection.GetValue( + nameof(ConnectionFactory.MaxInboundMessageBodySize)); + if (maxInboundMessageBodySize.HasValue) + { + connection.Value.MaxInboundMessageBodySize = maxInboundMessageBodySize.Value; + } + + connection.Value.AutomaticRecoveryEnabled = false; } }); } diff --git a/framework/test/Volo.Abp.RabbitMQ.Tests/Volo/Abp/RabbitMQ/AbpRabbitMqOptions_Tests.cs b/framework/test/Volo.Abp.RabbitMQ.Tests/Volo/Abp/RabbitMQ/AbpRabbitMqOptions_Tests.cs new file mode 100644 index 0000000000..7ec918b5fd --- /dev/null +++ b/framework/test/Volo.Abp.RabbitMQ.Tests/Volo/Abp/RabbitMQ/AbpRabbitMqOptions_Tests.cs @@ -0,0 +1,129 @@ +using System.IO; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using RabbitMQ.Client; +using Shouldly; +using Xunit; + +namespace Volo.Abp.RabbitMQ; + +public class AbpRabbitMqOptions_Tests +{ + [Fact] + public void Should_Bind_Connection_Settings_From_Json() + { + var connection = GetConnection( + """ + { + "RabbitMQ": { + "Connections": { + "Default": { + "HostName": "123.123.123.123", + "Port": 5672, + "MaxInboundMessageBodySize": 500000000 + } + } + } + } + """); + + connection.HostName.ShouldBe("123.123.123.123"); + connection.Port.ShouldBe(5672); + connection.MaxInboundMessageBodySize.ShouldBe(500000000u); + } + + [Fact] + public void Should_Bind_Ssl_Settings_From_Json() + { + var connection = GetConnection( + """ + { + "RabbitMQ": { + "Connections": { + "Default": { + "HostName": "rabbit.example.test", + "Ssl": { + "AcceptablePolicyErrors": "RemoteCertificateChainErrors", + "CertPassphrase": "secret", + "CheckCertificateRevocation": true, + "Enabled": true, + "ServerName": "tls.example.test", + "Version": "Tls12" + } + } + } + } + } + """); + + connection.Ssl.AcceptablePolicyErrors.ShouldBe(SslPolicyErrors.RemoteCertificateChainErrors); + connection.Ssl.CertPassphrase.ShouldBe("secret"); + connection.Ssl.CheckCertificateRevocation.ShouldBeTrue(); + connection.Ssl.Enabled.ShouldBeTrue(); + connection.Ssl.ServerName.ShouldBe("tls.example.test"); + connection.Ssl.Version.ShouldBe(SslProtocols.Tls12); + } + + [Fact] + public void Should_Combine_Uri_With_Advanced_Json_Connection_Settings() + { + var connection = GetConnection( + """ + { + "RabbitMQ": { + "Connections": { + "Default": { + "Uri": "amqps://configured-user:configured-pass@uri.example.test:5678/configured-vhost", + "MaxInboundMessageBodySize": 500000000, + "Ssl": { + "AcceptablePolicyErrors": "RemoteCertificateChainErrors", + "CertPassphrase": "secret", + "CheckCertificateRevocation": true, + "Enabled": true, + "ServerName": "tls.example.test", + "Version": "Tls12" + } + } + } + } + } + """); + + connection.HostName.ShouldBe("uri.example.test"); + connection.Port.ShouldBe(5678); + connection.UserName.ShouldBe("configured-user"); + connection.Password.ShouldBe("configured-pass"); + connection.VirtualHost.ShouldBe("configured-vhost"); + connection.MaxInboundMessageBodySize.ShouldBe(500000000u); + connection.Ssl.AcceptablePolicyErrors.ShouldBe(SslPolicyErrors.RemoteCertificateChainErrors); + connection.Ssl.CertPassphrase.ShouldBe("secret"); + connection.Ssl.CheckCertificateRevocation.ShouldBeTrue(); + connection.Ssl.Enabled.ShouldBeTrue(); + connection.Ssl.ServerName.ShouldBe("tls.example.test"); + connection.Ssl.Version.ShouldBe(SslProtocols.Tls12); + } + + private static ConnectionFactory GetConnection( + string json, + string connectionName = RabbitMqConnections.DefaultConnectionName) + { + using var application = AbpApplicationFactory.Create(options => + { + options.Services.ReplaceConfiguration( + new ConfigurationBuilder() + .AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) + .Build()); + }); + + application.Initialize(); + + return application.ServiceProvider + .GetRequiredService>() + .Value + .Connections[connectionName]; + } +}