diff --git a/src/Messaging/NBB.Messaging.Host/Builder/MessagingHostConfigurationBuilder.cs b/src/Messaging/NBB.Messaging.Host/Builder/MessagingHostConfigurationBuilder.cs index 4b069671..1c474a54 100644 --- a/src/Messaging/NBB.Messaging.Host/Builder/MessagingHostConfigurationBuilder.cs +++ b/src/Messaging/NBB.Messaging.Host/Builder/MessagingHostConfigurationBuilder.cs @@ -38,8 +38,11 @@ public IMessagingHostOptionsBuilder AddSubscriberServices(Action(); - _subscriberGroups.Add(_currentSubscriberGroup); + if (_currentSubscriberGroup == null) + { + _currentSubscriberGroup = new List(); + _subscriberGroups.Add(_currentSubscriberGroup); + } return this; } @@ -143,7 +146,7 @@ IMessagingHostPipelineBuilder WithOptions( /// /// Used to subscriberBuilder the messaging host pipeline /// - public interface IMessagingHostPipelineBuilder + public interface IMessagingHostPipelineBuilder : IMessagingHostConfigurationBuilder { /// /// Adds the message processing pipeline to the messaging host. @@ -152,4 +155,4 @@ public interface IMessagingHostPipelineBuilder /// The messaging host subscriberBuilder to further subscriberBuilder the messaging host. It is used in the fluent API void UsePipeline(Action> configurePipeline); } -} \ No newline at end of file +} diff --git a/src/Messaging/NBB.Messaging.Host/README.md b/src/Messaging/NBB.Messaging.Host/README.md index 69df48f0..1c53f516 100644 --- a/src/Messaging/NBB.Messaging.Host/README.md +++ b/src/Messaging/NBB.Messaging.Host/README.md @@ -73,7 +73,28 @@ services.AddMessagingHost( ); } ); -``` +``` + +#### Multiple subscriber groups using the same pipeline + +In case you need to use the same pipeline for more subscriber groups, the builder lets you chain multiple `AddSubscriberServices(...).WithOptions(...)` function calls: + +```csharp + +services.AddMessagingHost( + Configuration, + hostBuilder => + { + hostBuilder.Configure(configBuilder => configBuilder + .AddSubscriberServices(...) + .WithOptions(...) + .AddSubscriberServices(...) + .WithOptions(...) + .UsePipeline(...) + ); + } +); +``` #### Advanced scenarios There is an overload of the *Configure* method that allows async/await operations. diff --git a/test/UnitTests/Messaging/NBB.Messaging.Host.Tests/MessagingHostBuilderTests.cs b/test/UnitTests/Messaging/NBB.Messaging.Host.Tests/MessagingHostBuilderTests.cs index 78bbf854..510e8bad 100644 --- a/test/UnitTests/Messaging/NBB.Messaging.Host.Tests/MessagingHostBuilderTests.cs +++ b/test/UnitTests/Messaging/NBB.Messaging.Host.Tests/MessagingHostBuilderTests.cs @@ -5,6 +5,7 @@ using MediatR; using Microsoft.Extensions.DependencyInjection; using Moq; +using NBB.Core.Pipeline; using NBB.Messaging.Abstractions; using System; using System.Collections.Generic; @@ -147,6 +148,29 @@ public void Should_register_handled_topics_singleton() config.Subscribers[0].Pipeline.Should().NotBeNull(); } + [Fact] + public void Should_register_same_pipeline_for_more_subscriber_groups() + { + //Arrange + var services = Mock.Of(); + var provider = Mock.Of(); + + //Act + var builder = new MessagingHostConfigurationBuilder(provider, services); + builder + .AddSubscriberServices(cfg => cfg.FromTopics("SomeTopicName")).WithDefaultOptions() + .AddSubscriberServices(cfg => cfg.FromTopics("OtherTopicName")).WithDefaultOptions() + .UsePipeline(_ => { }); + var config = builder.Build(); + + //Assert + config.Subscribers.Should().HaveCount(2); + config.Subscribers[0].Options.Should().Be(MessagingSubscriberOptions.Default with { TopicName = "SomeTopicName" }); + config.Subscribers[1].Options.Should().Be(MessagingSubscriberOptions.Default with { TopicName = "OtherTopicName" }); + config.Subscribers[0].Pipeline.Should().Be(config.Subscribers[1].Pipeline); + + } + public record CommandMessage : IRequest; public record EventMessage : INotification; @@ -186,4 +210,4 @@ public Task Handle(QueryMessage request, CancellationToken cancellationT } } } -} \ No newline at end of file +}