Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .autover/changes/7affc078-3e1b-4baa-a27b-67280b0e95d5.json
Original file line number Diff line number Diff line change
@@ -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."
]
}
]
}
22 changes: 16 additions & 6 deletions src/AWS.Messaging/Configuration/MessageBusBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace AWS.Messaging.Configuration;
/// </summary>
public class MessageBusBuilder : IMessageBusBuilder
{
private static readonly Dictionary<IServiceCollection, MessageConfiguration> _messageConfigurations = new();
private readonly MessageConfiguration _messageConfiguration;
private readonly IList<ServiceDescriptor> _additionalServices = new List<ServiceDescriptor>();
private readonly IServiceCollection _serviceCollection;
Expand All @@ -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;
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -129,7 +138,6 @@ public IMessageBusBuilder AddSQSPoller(string queueUrl, Action<SQSMessagePollerO
VisibilityTimeoutExtensionHeartbeatInterval = sqsMessagePollerOptions.VisibilityTimeoutExtensionHeartbeatInterval,
WaitTimeSeconds = sqsMessagePollerOptions.WaitTimeSeconds,
IsExceptionFatal = sqsMessagePollerOptions.IsExceptionFatal

};

_messageConfiguration.MessagePollerConfigurations.Add(sqsMessagePollerConfiguration);
Expand Down Expand Up @@ -308,7 +316,7 @@ internal void Build()
_serviceCollection.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, NullLoggerFactory>());
_serviceCollection.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(NullLogger<>)));

_serviceCollection.AddSingleton<IMessageConfiguration>(_messageConfiguration);
_serviceCollection.TryAddSingleton<IMessageConfiguration>(_messageConfiguration);
_serviceCollection.TryAddSingleton<IMessageSerializer, MessageSerializer>();
_serviceCollection.TryAddSingleton<IEnvelopeSerializer, EnvelopeSerializer>();
_serviceCollection.TryAddSingleton<IDateTimeHandler, DateTimeHandler>();
Expand All @@ -326,18 +334,20 @@ internal void Build()

if (_messageConfiguration.PublisherMappings.Any())
{
_serviceCollection.AddSingleton<IMessagePublisher, MessageRoutingPublisher>();
_serviceCollection.TryAddSingleton<IMessagePublisher, MessageRoutingPublisher>();

if (_messageConfiguration.PublisherMappings.Any(x => x.PublishTargetType == PublisherTargetType.SQS_PUBLISHER))
{
_serviceCollection.TryAddAWSService<Amazon.SQS.IAmazonSQS>();
_serviceCollection.TryAddSingleton<ISQSPublisher, SQSPublisher>();
}

if (_messageConfiguration.PublisherMappings.Any(x => x.PublishTargetType == PublisherTargetType.SNS_PUBLISHER))
{
_serviceCollection.TryAddAWSService<Amazon.SimpleNotificationService.IAmazonSimpleNotificationService>();
_serviceCollection.TryAddSingleton<ISNSPublisher, SNSPublisher>();
}

if (_messageConfiguration.PublisherMappings.Any(x => x.PublishTargetType == PublisherTargetType.EVENTBRIDGE_PUBLISHER))
{
_serviceCollection.TryAddAWSService<Amazon.EventBridge.IAmazonEventBridge>();
Expand All @@ -351,7 +361,7 @@ internal void Build()

foreach (var subscriberMapping in _messageConfiguration.SubscriberMappings)
{
_serviceCollection.AddScoped(subscriberMapping.HandlerType);
_serviceCollection.TryAddScoped(subscriberMapping.HandlerType);
}
}

Expand Down Expand Up @@ -387,7 +397,7 @@ internal void Build()

foreach (var service in _additionalServices)
{
_serviceCollection.Add(service);
_serviceCollection.TryAdd(service);
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/AWS.Messaging.UnitTests/MessageBusBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ public void BuildMessageBus()
CheckRequiredServices(serviceProvider);
}

[Fact]
public void BuildMessageBus_MultipleInvocations()
{
_serviceCollection.AddAWSMessageBus(builder =>
{
builder.AddSQSPublisher<OrderInfo>("sqsQueueUrl");
builder.AddMessageHandler<AddressInfoHandler, AddressInfo>();
});

_serviceCollection.AddAWSMessageBus(builder =>
{
builder.AddMessageHandler<ChatMessageHandler, ChatMessage>();
});

var serviceProvider = _serviceCollection.BuildServiceProvider();

var messagePublisher = serviceProvider.GetService<IMessagePublisher>();
Assert.NotNull(messagePublisher);

CheckRequiredServices(serviceProvider);

var mesageConfiguration = serviceProvider.GetRequiredService<IMessageConfiguration>();
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()
{
Expand Down