Overview
Savvyio.Extensions.DependencyInjection.NATS wires the NATS transport from Savvyio.Extensions.NATS into IServiceCollection for Savvy I/O command queues and integration event buses. It adds registration methods for the default queue and bus types plus marker-specific variants, so consumers resolve NATS-backed channels through Savvy I/O abstractions instead of concrete transport classes.
The package does not implement a transport on its own. It composes the existing NATS types with the dependency injection helpers from Savvyio.Extensions.DependencyInjection, including singleton defaults and configured options registration.
Key APIs
AddNatsCommandQueue registers the non-marker NatsCommandQueue as a message queue for ICommand and stores the supplied NatsCommandQueueOptions in the container. The implementation defaults to ServiceLifetime.Singleton, and the tests show the registration resolves through IPointToPointChannel<ICommand>, ISender<ICommand>, and IReceiver<ICommand>.
AddNatsCommandQueue<TMarker> registers the marker-aware NatsCommandQueue<TMarker> and forwards it to the marker-specific command channel interfaces. Use this overload when the container needs a distinct queue identity such as IPointToPointChannel<ICommand, TMarker>.
AddNatsEventBus registers the non-marker NatsEventBus as the publish-subscribe channel for IIntegrationEvent and stores the supplied NatsEventBusOptions. The tests verify that the same registration is available through IPublishSubscribeChannel<IIntegrationEvent>, IPublisher<IIntegrationEvent>, and ISubscriber<IIntegrationEvent>.
AddNatsEventBus<TMarker> registers the marker-aware NatsEventBus<TMarker> for IPublishSubscribeChannel<IIntegrationEvent, TMarker> and the related publisher and subscriber interfaces. It mirrors the non-generic overload while keeping the registration scoped by marker type.
NatsCommandQueue<TMarker> derives from Savvyio.Extensions.NATS.Commands.NatsCommandQueue and implements IPointToPointChannel<ICommand, TMarker>. Its constructor keeps the lower-level IMarshaller and NatsCommandQueueOptions<TMarker> contract while making the queue visible to the marker-based DI surface.
NatsCommandQueueOptions<TMarker> inherits Savvyio.Extensions.NATS.Commands.NatsCommandQueueOptions and adds IDependencyInjectionMarker<TMarker>. The wrapper does not introduce new properties, so queue setup still comes from the lower-level options such as Subject, StreamName, and ConsumerName.
NatsEventBus<TMarker> derives from Savvyio.Extensions.NATS.EventDriven.NatsEventBus and implements IPublishSubscribeChannel<IIntegrationEvent, TMarker>. It is the marker-aware counterpart that lets DI resolve a NATS event bus through the marker-specific publish-subscribe interfaces.
NatsEventBusOptions<TMarker> inherits Savvyio.Extensions.NATS.EventDriven.NatsEventBusOptions and adds IDependencyInjectionMarker<TMarker>. Like the queue options wrapper, it keeps the lower-level NATS configuration model intact while binding the options to a marker type.
Basic usage
using System.Linq;
using Codebelt.Extensions.Xunit;
using Microsoft.Extensions.DependencyInjection;
using Savvyio.Extensions.DependencyInjection.NATS;
using Xunit;
namespace MyProject.Tests;
public class NatsEventBusRegistrationTests : Test
{
public NatsEventBusRegistrationTests(ITestOutputHelper output) : base(output) { }
[Fact]
public void AddNatsEventBus_ConfiguredMarker_RegistersServices()
{
var services = new ServiceCollection();
services.AddNatsEventBus<PaymentsMarker>(options => options.Subject = "payments.events");
TestOutput.WriteLine($"Registered services: {services.Count}");
Assert.Contains(services, d => d.ServiceType.Name.Contains("IPublishSubscribeChannel"));
Assert.Contains(services, d => d.ServiceType.Name.Contains("IPublisher"));
Assert.Contains(services, d => d.ServiceType.Name.Contains("ISubscriber"));
}
}
public readonly struct PaymentsMarker { }
Use this pattern when you want to verify the NATS event bus registrations before building a container. It matters because the package contributes the marker-specific publish-subscribe service descriptors and typed option setup in one call.
Installation
dotnet add package Savvyio.Extensions.DependencyInjection.NATS
Usage guidance
Use this package when your application already models commands or integration events with Savvy I/O abstractions and you want IServiceCollection to expose NATS-backed channels, especially when marker types separate multiple registrations in the same container. Register an IMarshaller before resolving these services, and configure Subject for both buses and queues plus StreamName and ConsumerName for queues because the underlying NATS options validate those values. If you do not need Microsoft DI registration or Savvy I/O channel abstractions, use Savvyio.Extensions.NATS or the plain NATS client APIs directly.
Family packages
- 🏭Savvyio.App
- 📦Savvyio.Commands
- 📦Savvyio.Commands.Messaging
- 📦Savvyio.Core
- 📦Savvyio.Domain
- 📦Savvyio.Domain.EventSourcing
- 📦Savvyio.EventDriven
- 📦Savvyio.EventDriven.Messaging
- 📦Savvyio.Extensions.Dapper
- 📦Savvyio.Extensions.DapperExtensions
- 📦Savvyio.Extensions.DependencyInjection
- 📦Savvyio.Extensions.DependencyInjection.Dapper
- 📦Savvyio.Extensions.DependencyInjection.DapperExtensions
- 📦Savvyio.Extensions.DependencyInjection.Domain
- 📦Savvyio.Extensions.DependencyInjection.EFCore
- 📦Savvyio.Extensions.DependencyInjection.EFCore.Domain
- 📦Savvyio.Extensions.DependencyInjection.EFCore.Domain.EventSourcing
- 📝Savvyio.Extensions.DependencyInjection.Newtonsoft.Json
- 📦Savvyio.Extensions.DependencyInjection.QueueStorage
- 📦Savvyio.Extensions.DependencyInjection.RabbitMQ
- 📦Savvyio.Extensions.DependencyInjection.SimpleQueueService
- 📝Savvyio.Extensions.DependencyInjection.Text.Json
- 📦Savvyio.Extensions.Dispatchers
- 📦Savvyio.Extensions.EFCore
- 📦Savvyio.Extensions.EFCore.Domain
- 📦Savvyio.Extensions.EFCore.Domain.EventSourcing
- 📦Savvyio.Extensions.NATS
- 📝Savvyio.Extensions.Newtonsoft.Json
- 📦Savvyio.Extensions.QueueStorage
- 📦Savvyio.Extensions.RabbitMQ
- 📦Savvyio.Extensions.SimpleQueueService
- 📝Savvyio.Extensions.Text.Json
- 📦Savvyio.Messaging
- 📦Savvyio.Queries