|
| 1 | +using CCE.Application.Notifications.Messages; |
| 2 | +using MassTransit; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | + |
| 7 | +namespace CCE.Infrastructure.Notifications.Messaging; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Registers MassTransit with the correct transport based on |
| 11 | +/// <c>appsettings.json → Messaging:Transport</c>: |
| 12 | +/// |
| 13 | +/// <list type="table"> |
| 14 | +/// <item><term>InMemory</term><description>No broker. Messages flow in-process via a channel. Use for local dev and all tests.</description></item> |
| 15 | +/// <item><term>RabbitMQ</term><description>Production. Requires <c>Messaging:RabbitMqHost</c> and a running broker.</description></item> |
| 16 | +/// </list> |
| 17 | +/// |
| 18 | +/// Call <c>services.AddCceMessaging(configuration)</c> from |
| 19 | +/// <see cref="CCE.Infrastructure.DependencyInjection.AddInfrastructure"/>. |
| 20 | +/// </summary> |
| 21 | +public static class MessagingServiceExtensions |
| 22 | +{ |
| 23 | + public static IServiceCollection AddCceMessaging( |
| 24 | + this IServiceCollection services, |
| 25 | + IConfiguration configuration) |
| 26 | + { |
| 27 | + services.AddOptions<MessagingOptions>() |
| 28 | + .Bind(configuration.GetSection(MessagingOptions.SectionName)) |
| 29 | + .ValidateDataAnnotations() |
| 30 | + .ValidateOnStart(); |
| 31 | + |
| 32 | + var options = configuration |
| 33 | + .GetSection(MessagingOptions.SectionName) |
| 34 | + .Get<MessagingOptions>() ?? new MessagingOptions(); |
| 35 | + |
| 36 | + services.AddMassTransit(x => |
| 37 | + { |
| 38 | + // Register consumer + its definition (retry policy, concurrency). |
| 39 | + x.AddConsumer<NotificationMessageConsumer, NotificationMessageConsumerDefinition>(); |
| 40 | + |
| 41 | + switch (options.Transport.ToUpperInvariant()) |
| 42 | + { |
| 43 | + case "RABBITMQ": |
| 44 | + x.UsingRabbitMq((ctx, cfg) => |
| 45 | + { |
| 46 | + cfg.Host(options.RabbitMqHost ?? "amqp://guest:guest@localhost", options.RabbitMqVirtualHost, h => |
| 47 | + { |
| 48 | + // Credentials are embedded in RabbitMqHost URI or set here. |
| 49 | + // Production: use environment variables / Azure Key Vault secrets. |
| 50 | + }); |
| 51 | + |
| 52 | + // Auto-configure endpoints from consumer definitions. |
| 53 | + cfg.ConfigureEndpoints(ctx); |
| 54 | + }); |
| 55 | + break; |
| 56 | + |
| 57 | + default: // "InMemory" or missing |
| 58 | + x.UsingInMemory((ctx, cfg) => |
| 59 | + { |
| 60 | + cfg.ConfigureEndpoints(ctx); |
| 61 | + }); |
| 62 | + break; |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + // Replace the synchronous in-process dispatcher with the async bus publisher |
| 67 | + // only when UseAsyncDispatcher=true (default). |
| 68 | + if (options.UseAsyncDispatcher) |
| 69 | + { |
| 70 | + // Remove the InProcessNotificationMessageDispatcher registered in DependencyInjection.cs |
| 71 | + var descriptor = services.FirstOrDefault( |
| 72 | + d => d.ServiceType == typeof(INotificationMessageDispatcher)); |
| 73 | + if (descriptor is not null) |
| 74 | + services.Remove(descriptor); |
| 75 | + |
| 76 | + services.AddScoped<INotificationMessageDispatcher, |
| 77 | + MassTransitNotificationMessageDispatcher>(); |
| 78 | + } |
| 79 | + |
| 80 | + return services; |
| 81 | + } |
| 82 | +} |
0 commit comments