| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Transports.RabbitMQ; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | |
| | | 5 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// DI registration for the RabbitMQ AsyncResponse transport. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class RabbitMqAsyncResponseServiceCollectionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Registers RabbitMQ as the worker transport and response ingress in one call: |
| | | 14 | | /// <list type="bullet"> |
| | | 15 | | /// <item><description>worker jobs are published to <see cref="RabbitMqAsyncResponseOptions.WorkerExchange"/> with < |
| | | 16 | | /// <item><description>a hosted consumer reads <see cref="RabbitMqAsyncResponseOptions.WorkerQueue"/> and executes w |
| | | 17 | | /// <item><description>a hosted consumer reads <see cref="RabbitMqAsyncResponseOptions.ResponseQueue"/> and feeds re |
| | | 18 | | /// </list> |
| | | 19 | | /// RabbitMQ is a transport, not a recovery store: pair it with a channel |
| | | 20 | | /// (<c>.WithInMemoryChannel()</c> for simple apps, or <c>.WithRedisChannel()</c> when late |
| | | 21 | | /// responses must survive redeploys), which provides the waiter side and recovery state. |
| | | 22 | | /// </summary> |
| | | 23 | | public static AsyncResponseRegistrationBuilder WithRabbitMqTransport( |
| | | 24 | | this AsyncResponseRegistrationBuilder builder, |
| | | 25 | | Action<RabbitMqAsyncResponseOptions> configure) |
| | | 26 | | { |
| | 3 | 27 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 28 | | |
| | 3 | 29 | | var services = builder.Services; |
| | 3 | 30 | | services.AddOptions(); |
| | 3 | 31 | | services.Configure(configure); |
| | | 32 | | |
| | 3 | 33 | | services.TryAddSingleton<RabbitMqWorkerTransport>(); |
| | 3 | 34 | | services.Replace(ServiceDescriptor.Singleton<IWorkerTransport>(provider => |
| | 3 | 35 | | provider.GetRequiredService<RabbitMqWorkerTransport>())); |
| | 3 | 36 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseReplyTargetProvider, RabbitMqReplyTargetProvider>()); |
| | 3 | 37 | | services.AddSingleton(provider => |
| | 3 | 38 | | { |
| | 3 | 39 | | // Resolved ack modes declared to the Core startup validator, which vetoes early ACK on |
| | 3 | 40 | | // the worker queue durable-flow wake-ups ride (see AsyncResponseStartupValidator). |
| | 3 | 41 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<RabbitMqAsyncResponseOptions |
| | 3 | 42 | | return new AsyncResponseTransportMarker("RabbitMQ") |
| | 3 | 43 | | { |
| | 3 | 44 | | WorkerSubscriberUsesEarlyAck = options.WorkerSubscriber.AckMode == RabbitMqAckMode.AckAfterEnqueue, |
| | 3 | 45 | | WorkerAckModePath = $"{nameof(RabbitMqAsyncResponseOptions)}.{nameof(options.WorkerSubscriber)}.{nameof( |
| | 3 | 46 | | ResponseSubscriberUsesEarlyAck = options.ResponseSubscriber.AckMode == RabbitMqAckMode.AckAfterEnqueue, |
| | 3 | 47 | | ResponseAckModePath = $"{nameof(RabbitMqAsyncResponseOptions)}.{nameof(options.ResponseSubscriber)}.{nam |
| | 3 | 48 | | }; |
| | 3 | 49 | | }); |
| | | 50 | | |
| | 3 | 51 | | services.AddHostedService<RabbitMqWorkerSubscriber>(); |
| | 3 | 52 | | services.AddHostedService<RabbitMqResponseIngressSubscriber>(); |
| | | 53 | | |
| | 3 | 54 | | return builder; |
| | | 55 | | } |
| | | 56 | | } |