diff --git a/.autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json b/.autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json new file mode 100644 index 00000000..4a5a2dce --- /dev/null +++ b/.autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json @@ -0,0 +1,11 @@ +{ + "Projects": [ + { + "Name": "AWS.Messaging", + "Type": "Patch", + "ChangelogMessages": [ + "Add support for AddAWSMessageBus being invoked multiple times against the same ServiceCollection. This allows different modules to register their own handlers rather than requiring a centralized registration." + ] + } + ] +} \ No newline at end of file diff --git a/src/AWS.Messaging/Configuration/MessageBusBuilder.cs b/src/AWS.Messaging/Configuration/MessageBusBuilder.cs index 301cb014..22a4dd23 100644 --- a/src/AWS.Messaging/Configuration/MessageBusBuilder.cs +++ b/src/AWS.Messaging/Configuration/MessageBusBuilder.cs @@ -25,6 +25,7 @@ namespace AWS.Messaging.Configuration; /// public class MessageBusBuilder : IMessageBusBuilder { + private static readonly Dictionary _messageConfigurations = new(); private readonly MessageConfiguration _messageConfiguration; private readonly IList _additionalServices = new List(); private readonly IServiceCollection _serviceCollection; @@ -35,7 +36,15 @@ public class MessageBusBuilder : IMessageBusBuilder public MessageBusBuilder(IServiceCollection services) { _serviceCollection = services; - _messageConfiguration = new MessageConfiguration(); + if (_messageConfigurations.TryGetValue(services, out var config)) + { + _messageConfiguration = config; + } + else + { + _messageConfiguration = new MessageConfiguration(); + _messageConfigurations[services] = _messageConfiguration; + } } /// @@ -129,7 +138,6 @@ public IMessageBusBuilder AddSQSPoller(string queueUrl, Action()); _serviceCollection.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(NullLogger<>))); - _serviceCollection.AddSingleton(_messageConfiguration); + _serviceCollection.TryAddSingleton(_messageConfiguration); _serviceCollection.TryAddSingleton(); _serviceCollection.TryAddSingleton(); _serviceCollection.TryAddSingleton(); @@ -326,18 +334,20 @@ internal void Build() if (_messageConfiguration.PublisherMappings.Any()) { - _serviceCollection.AddSingleton(); + _serviceCollection.TryAddSingleton(); if (_messageConfiguration.PublisherMappings.Any(x => x.PublishTargetType == PublisherTargetType.SQS_PUBLISHER)) { _serviceCollection.TryAddAWSService(); _serviceCollection.TryAddSingleton(); } + if (_messageConfiguration.PublisherMappings.Any(x => x.PublishTargetType == PublisherTargetType.SNS_PUBLISHER)) { _serviceCollection.TryAddAWSService(); _serviceCollection.TryAddSingleton(); } + if (_messageConfiguration.PublisherMappings.Any(x => x.PublishTargetType == PublisherTargetType.EVENTBRIDGE_PUBLISHER)) { _serviceCollection.TryAddAWSService(); @@ -351,7 +361,7 @@ internal void Build() foreach (var subscriberMapping in _messageConfiguration.SubscriberMappings) { - _serviceCollection.AddScoped(subscriberMapping.HandlerType); + _serviceCollection.TryAddScoped(subscriberMapping.HandlerType); } } @@ -387,7 +397,7 @@ internal void Build() foreach (var service in _additionalServices) { - _serviceCollection.Add(service); + _serviceCollection.TryAdd(service); } } diff --git a/test/AWS.Messaging.UnitTests/MessageBusBuilderTests.cs b/test/AWS.Messaging.UnitTests/MessageBusBuilderTests.cs index 9059ce7a..ffb87c96 100644 --- a/test/AWS.Messaging.UnitTests/MessageBusBuilderTests.cs +++ b/test/AWS.Messaging.UnitTests/MessageBusBuilderTests.cs @@ -57,6 +57,35 @@ public void BuildMessageBus() CheckRequiredServices(serviceProvider); } + [Fact] + public void BuildMessageBus_MultipleInvocations() + { + _serviceCollection.AddAWSMessageBus(builder => + { + builder.AddSQSPublisher("sqsQueueUrl"); + builder.AddMessageHandler(); + }); + + _serviceCollection.AddAWSMessageBus(builder => + { + builder.AddMessageHandler(); + }); + + var serviceProvider = _serviceCollection.BuildServiceProvider(); + + var messagePublisher = serviceProvider.GetService(); + Assert.NotNull(messagePublisher); + + CheckRequiredServices(serviceProvider); + + var mesageConfiguration = serviceProvider.GetRequiredService(); + Assert.Equal(2, mesageConfiguration.SubscriberMappings.Count); + Assert.Equal(typeof(AddressInfo), mesageConfiguration.SubscriberMappings[0].MessageType); + Assert.Equal(typeof(AddressInfoHandler), mesageConfiguration.SubscriberMappings[0].HandlerType); + Assert.Equal(typeof(ChatMessage), mesageConfiguration.SubscriberMappings[1].MessageType); + Assert.Equal(typeof(ChatMessageHandler), mesageConfiguration.SubscriberMappings[1].HandlerType); + } + [Fact] public void MessageBus_ConfigureBackoffPolicy_Default() {