From 3919df22b56680be927679421cfb53a3c8ce42c2 Mon Sep 17 00:00:00 2001 From: Brendon Date: Thu, 15 Aug 2024 10:56:36 -0400 Subject: [PATCH 1/3] Use static MessageConfiguration tied to ServiceCollection (#156) --- .../7affc078-3e1b-4baa-a27b-67280b0e95d5.json | 11 +++++++ .../Configuration/MessageBusBuilder.cs | 22 ++++++++++---- .../MessageBusBuilderTests.cs | 29 +++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 .autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json 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() { From cf86f2c94f9455ce29d772a289812d4d6e33ecc3 Mon Sep 17 00:00:00 2001 From: aws-sdk-dotnet-automation Date: Thu, 15 Aug 2024 14:57:18 +0000 Subject: [PATCH 2/3] release_2024-08-15 --- src/AWS.Messaging/AWS.Messaging.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AWS.Messaging/AWS.Messaging.csproj b/src/AWS.Messaging/AWS.Messaging.csproj index b55ad616..6086a143 100644 --- a/src/AWS.Messaging/AWS.Messaging.csproj +++ b/src/AWS.Messaging/AWS.Messaging.csproj @@ -17,7 +17,7 @@ CA1727 true ..\..\public.snk - 0.9.1 + 0.9.2 true true true From bbdb9b63b3735d880c76edfbd81e6e785c084d4b Mon Sep 17 00:00:00 2001 From: aws-sdk-dotnet-automation Date: Thu, 15 Aug 2024 14:57:18 +0000 Subject: [PATCH 3/3] Updated changelog --- .../changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json | 11 ----------- CHANGELOG.md | 5 +++++ 2 files changed, 5 insertions(+), 11 deletions(-) delete mode 100644 .autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json diff --git a/.autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json b/.autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json deleted file mode 100644 index 4a5a2dce..00000000 --- a/.autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "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/CHANGELOG.md b/CHANGELOG.md index 70051fea..cb74253a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## Release 2024-08-15 + +### AWS.Messaging (0.9.2) +* 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. + ## Release 2024-08-02 ### AWS.Messaging.Lambda (0.10.0)