| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Transports.NATS; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | using Microsoft.Extensions.Hosting; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | using NATS.Client.Core; |
| | | 8 | | using NATS.Net; |
| | | 9 | | |
| | | 10 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// DI registration for the NATS JetStream AsyncResponse transport. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class NatsAsyncResponseTransportServiceCollectionExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Registers NATS JetStream as the worker transport and response ingress in one call: |
| | | 19 | | /// <list type="bullet"> |
| | | 20 | | /// <item><description>worker jobs are published to the configured worker subject;</description></item> |
| | | 21 | | /// <item><description>a hosted durable-consumer subscriber reads worker messages and executes jobs;</description></ |
| | | 22 | | /// <item><description>a hosted durable-consumer subscriber reads response messages and feeds them into <see cref="I |
| | | 23 | | /// </list> |
| | | 24 | | /// NATS JetStream is a transport, not the waiter/recovery channel. Pair it with |
| | | 25 | | /// <c>.WithNatsChannel()</c> when late responses must survive redeploys, or with |
| | | 26 | | /// <c>.WithInMemoryChannel()</c> for simple single-process waits. |
| | | 27 | | /// <para> |
| | | 28 | | /// Requires a <c>NATS.Client.Core.INatsConnection</c> singleton registered by the host (for |
| | | 29 | | /// example via <c>builder.Services.AddNatsClient(...)</c>) and a NATS server with JetStream |
| | | 30 | | /// enabled. Reuse the same connection as the NATS channel package when both are installed. |
| | | 31 | | /// </para> |
| | | 32 | | /// </summary> |
| | | 33 | | public static AsyncResponseRegistrationBuilder WithNatsTransport( |
| | | 34 | | this AsyncResponseRegistrationBuilder builder, |
| | | 35 | | Action<NatsAsyncResponseTransportOptions> configure) |
| | | 36 | | { |
| | 3 | 37 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 38 | | |
| | 3 | 39 | | var services = builder.Services; |
| | 3 | 40 | | services.AddOptions(); |
| | 3 | 41 | | services.Configure(configure); |
| | | 42 | | |
| | | 43 | | // The worker transport and hosted subscribers each build their JetStream client adapter from the |
| | | 44 | | // host's INatsConnection via their public constructors (mirroring the other transport packages). |
| | 3 | 45 | | services.TryAddSingleton<NatsWorkerTransport>(); |
| | 3 | 46 | | services.Replace(ServiceDescriptor.Singleton<IWorkerTransport>(provider => |
| | 3 | 47 | | provider.GetRequiredService<NatsWorkerTransport>())); |
| | 3 | 48 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseReplyTargetProvider, NatsReplyTargetProvider>()); |
| | 3 | 49 | | services.AddSingleton(provider => |
| | 3 | 50 | | { |
| | 3 | 51 | | // Resolved ack modes declared to the Core startup validator, which vetoes early ACK on |
| | 3 | 52 | | // the worker queue durable-flow wake-ups ride (see AsyncResponseStartupValidator). |
| | 3 | 53 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<NatsAsyncResponseTransportOp |
| | 3 | 54 | | return new AsyncResponseTransportMarker(NatsAsyncResponseTransportOptions.TransportName) |
| | 3 | 55 | | { |
| | 3 | 56 | | WorkerSubscriberUsesEarlyAck = options.WorkerSubscriber.AckMode == NatsAckMode.AckAfterEnqueue, |
| | 3 | 57 | | WorkerAckModePath = $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.WorkerSubscriber)}.{na |
| | 3 | 58 | | ResponseSubscriberUsesEarlyAck = options.ResponseSubscriber.AckMode == NatsAckMode.AckAfterEnqueue, |
| | 3 | 59 | | ResponseAckModePath = $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.ResponseSubscriber)} |
| | 3 | 60 | | }; |
| | 3 | 61 | | }); |
| | | 62 | | |
| | 3 | 63 | | services.AddHostedService<NatsWorkerSubscriber>(); |
| | 3 | 64 | | services.AddHostedService<NatsResponseIngressSubscriber>(); |
| | | 65 | | |
| | 3 | 66 | | return builder; |
| | | 67 | | } |
| | | 68 | | } |