< Summary - AsyncResponse (Release / net8.0+net10.0 / unit+integration)

Information
Class: Microsoft.Extensions.DependencyInjection.NatsAsyncResponseTransportServiceCollectionExtensions
Assembly: AsyncResponse.Transports.NATS
File(s): /home/runner/work/AsyncResponse/AsyncResponse/src/Transports/AsyncResponse.Transports.NATS/ServiceCollectionExtensions.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 68
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WithNatsTransport(...)100%44100%

File(s)

/home/runner/work/AsyncResponse/AsyncResponse/src/Transports/AsyncResponse.Transports.NATS/ServiceCollectionExtensions.cs

#LineLine coverage
 1using AsyncResponse;
 2using AsyncResponse.Transports.NATS;
 3using Microsoft.Extensions.DependencyInjection.Extensions;
 4using Microsoft.Extensions.Hosting;
 5using Microsoft.Extensions.Logging;
 6using Microsoft.Extensions.Options;
 7using NATS.Client.Core;
 8using NATS.Net;
 9
 10namespace Microsoft.Extensions.DependencyInjection;
 11
 12/// <summary>
 13/// DI registration for the NATS JetStream AsyncResponse transport.
 14/// </summary>
 15public 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    {
 337        ArgumentNullException.ThrowIfNull(configure);
 38
 339        var services = builder.Services;
 340        services.AddOptions();
 341        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).
 345        services.TryAddSingleton<NatsWorkerTransport>();
 346        services.Replace(ServiceDescriptor.Singleton<IWorkerTransport>(provider =>
 347            provider.GetRequiredService<NatsWorkerTransport>()));
 348        services.Replace(ServiceDescriptor.Singleton<IAsyncResponseReplyTargetProvider, NatsReplyTargetProvider>());
 349        services.AddSingleton(provider =>
 350        {
 351            // Resolved ack modes declared to the Core startup validator, which vetoes early ACK on
 352            // the worker queue durable-flow wake-ups ride (see AsyncResponseStartupValidator).
 353            var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<NatsAsyncResponseTransportOp
 354            return new AsyncResponseTransportMarker(NatsAsyncResponseTransportOptions.TransportName)
 355            {
 356                WorkerSubscriberUsesEarlyAck = options.WorkerSubscriber.AckMode == NatsAckMode.AckAfterEnqueue,
 357                WorkerAckModePath = $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.WorkerSubscriber)}.{na
 358                ResponseSubscriberUsesEarlyAck = options.ResponseSubscriber.AckMode == NatsAckMode.AckAfterEnqueue,
 359                ResponseAckModePath = $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.ResponseSubscriber)}
 360            };
 361        });
 62
 363        services.AddHostedService<NatsWorkerSubscriber>();
 364        services.AddHostedService<NatsResponseIngressSubscriber>();
 65
 366        return builder;
 67    }
 68}