feat(notification): make messaging consumer configurable

Replace the hard-coded RabbitMQ setup with NotificationOptions bound from a configurable section. The new ConsumerType switch wires either the HTTP API client or the RabbitMQ client, and startup now fails with a clear error when required settings are missing.

Bump DigitalData.MessagingService.Client to 3.0.1 for the abstraction used by both transports.
This commit is contained in:
2026-11-05 12:03:26 +01:00
parent ce7e70162b
commit 2913514da8
4 changed files with 56 additions and 27 deletions

View File

@@ -0,0 +1,7 @@
namespace EnvelopeGenerator.Infrastructure.Notification.Configuration;
public enum ConsumerType
{
API,
RabbitMQ
}

View File

@@ -0,0 +1,16 @@
using DigitalData.MessagingService.RabbitMQ;
namespace EnvelopeGenerator.Infrastructure.Notification.Configuration;
public class NotificationOptions
{
public const string SectionName = "Notification";
public ConsumerType ConsumerType { get; set; }
public string? ApiUrl { get; set; }
public string SenderEmail { get; set; } = null!;
public RabbitMqConnectionConfiguration? RabbitMQ { get; set; }
}

View File

@@ -1,41 +1,47 @@
using DigitalData.MessagingService.Client; using DigitalData.MessagingService.Client;
using DigitalData.MessagingService.Domain.Entities;
using DigitalData.MessagingService.RabbitMQ;
using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification; using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
using EnvelopeGenerator.Infrastructure.Notification.Configuration;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EnvelopeGenerator.Infrastructure.Notification; namespace EnvelopeGenerator.Infrastructure.Notification;
public static class DependencyInjection public static class DependencyInjection
{ {
public static IServiceCollection AddNotificationInfrastructure(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddNotificationInfrastructure(this IServiceCollection services, IConfiguration configuration, string sectionName = NotificationOptions.SectionName)
{ {
var rMqConfig = configuration.GetSection("RabbitMQ").Get<RabbitMqConfiguration>() var ntfConfiguration = configuration.GetSection(sectionName);
?? throw new ArgumentNullException(nameof(RabbitMqConfiguration), "RabbitMQ configuration is missing"); services.Configure<NotificationOptions>(ntfConfiguration);
EmailSender.ConnectRabbitMq(config => var options = ntfConfiguration.Get<NotificationOptions>()
?? throw new InvalidOperationException(
$"Failed to bind configuration section '{sectionName}' to {nameof(NotificationOptions)}. Ensure the section exists and is correctly configured.");
switch (options.ConsumerType)
{ {
config.HostName = rMqConfig.HostName; case ConsumerType.API:
config.Port = rMqConfig.Port; if (options.ApiUrl is string apiUrl)
config.UserName = rMqConfig.UserName; services.AddMessagingServiceHttpClient(opt =>
config.Password = rMqConfig.Password; {
opt.BaseAddress = new Uri(apiUrl);
config.VirtualHost = "/"; // Additional configuration for the MessagingServiceHttpClient can be added here if needed
config.QueueName = "emailprofiler.email.outbox"; });
config.ExchangeName = "emailprofiler.emails"; else
config.RoutingKey = "email.outbox"; throw new InvalidOperationException(
config.DlqQueueName = "emailprofiler.email.outbox.dlq"; $"MessagingServiceApiUrl must be provided when ClientType is set to Api in the '{sectionName}' configuration section.");
config.DlqExchangeName = "emailprofiler.emails.dlq"; break;
config.DlqRoutingKey = "email.outbox.dlq"; case ConsumerType.RabbitMQ:
}); if(options.RabbitMQ is not null)
services.AddMessagingServiceClient(ntfConfiguration, nameof(options.RabbitMQ));
services.Configure<EmailAccount>(configuration.GetSection("NotificationEmailAccount")); else
throw new InvalidOperationException(
$"Missing RabbitMQ connection configuration." +
$"When ClientType is set to RabbitMQ, a valid RabbitMQ configuration must be provided in the '{sectionName}' configuration section.");
break;
default:
throw new InvalidOperationException(
$"Unsupported ConsumerType '{options.ConsumerType}' in the '{sectionName}' configuration section.");
}
services.AddScoped<IEnvelopeNotificationService, EnvelopeNotificationService>(); services.AddScoped<IEnvelopeNotificationService, EnvelopeNotificationService>();
return services; return services;

View File

@@ -8,7 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DigitalData.MessagingService.Client" Version="1.0.5" /> <PackageReference Include="DigitalData.MessagingService.Client" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.10" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.10" />
</ItemGroup> </ItemGroup>