From 5fe38fa34d61438f7e881def39ad14889a1b5feb Mon Sep 17 00:00:00 2001 From: Lewis Renfrew Date: Tue, 12 May 2026 15:12:42 +0100 Subject: [PATCH] support multiple exchanges --- changelog.md | 5 +++++ src/ExchangeBinding.cs | 3 +++ src/Linn.Common.Messaging.RabbitMQ.csproj | 4 ++-- src/RabbitChannelConfiguration.cs | 20 +++++++++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 src/ExchangeBinding.cs diff --git a/changelog.md b/changelog.md index 71fdd15..8187dcb 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,9 @@ # Changelog +## [5.0.0] - 2026-05-12 +### *Changes (Breaking)* +- Target .NET 10 +- Add optional additionalBindings parameter to RabbitChannelConfiguration, allowing a consumer queue to bind to multiple external exchanges with specific routing keys +- Add ExchangeBinding record type ## [4.0.1] - 2026-04-02 ### *Changes - Add optional exchangeType parameter to RabbitChannelConfiguration diff --git a/src/ExchangeBinding.cs b/src/ExchangeBinding.cs new file mode 100644 index 0000000..c022aa2 --- /dev/null +++ b/src/ExchangeBinding.cs @@ -0,0 +1,3 @@ +namespace Linn.Common.Messaging.RabbitMQ; + +public record ExchangeBinding(string ExchangeName, string[] RoutingKeys); diff --git a/src/Linn.Common.Messaging.RabbitMQ.csproj b/src/Linn.Common.Messaging.RabbitMQ.csproj index ea88340..7b36b58 100644 --- a/src/Linn.Common.Messaging.RabbitMQ.csproj +++ b/src/Linn.Common.Messaging.RabbitMQ.csproj @@ -1,11 +1,11 @@  Utilities for requesting data from other (proxy) services. - net9.0 + net10.0 enable Linn.Common.Messaging.RabbitMQ Linn.Common.Messaging.RabbitMQ - 4.0.1 + 5.0.0 enable diff --git a/src/RabbitChannelConfiguration.cs b/src/RabbitChannelConfiguration.cs index d5aa9c3..30e961a 100644 --- a/src/RabbitChannelConfiguration.cs +++ b/src/RabbitChannelConfiguration.cs @@ -12,7 +12,8 @@ public class RabbitChannelConfiguration( string? dlxName = null, bool createProducerChannel = true, bool createConsumerChannel = true, - string exchangeType = ExchangeType.Direct) + string exchangeType = ExchangeType.Direct, + ExchangeBinding[]? additionalBindings = null) : IAsyncDisposable { private readonly string dlqName = string.IsNullOrEmpty(dlqName) ? queueName : dlqName; @@ -112,6 +113,23 @@ await setupChannel.QueueBindAsync( noWait: false, cancellationToken: cancellationToken); } + + if (additionalBindings != null) + { + foreach (var binding in additionalBindings) + { + foreach (var routingKey in binding.RoutingKeys) + { + await setupChannel.QueueBindAsync( + queue: $"{queueName}.q", + exchange: $"{binding.ExchangeName}.x", + routingKey: routingKey, + arguments: null, + noWait: false, + cancellationToken: cancellationToken); + } + } + } } }