diff --git a/CHANGELOG.md b/CHANGELOG.md
index 70051fe..cb74253 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)
diff --git a/src/AWS.Messaging/AWS.Messaging.csproj b/src/AWS.Messaging/AWS.Messaging.csproj
index b55ad61..6086a14 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
diff --git a/src/AWS.Messaging/Configuration/MessageBusBuilder.cs b/src/AWS.Messaging/Configuration/MessageBusBuilder.cs
index 301cb01..22a4dd2 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 9059ce7..ffb87c9 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()
{