Codebelt

Savvyio.Extensions.DependencyInjection.RabbitMQ

Register RabbitMQ-backed command queues and event buses in Microsoft DI, including marker-specific channel resolution.

.NET 10.0 / .NET 9.0 MIT v5.0.8 3,268 downloads

Overview

Savvyio.Extensions.DependencyInjection.RabbitMQ wires the RabbitMQ transport from Savvyio.Extensions.RabbitMQ 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 RabbitMQ-backed channels through Savvy I/O abstractions instead of concrete transport classes.

The package does not implement the transport itself. It composes the existing RabbitMQ queue and bus types with the dependency injection helpers from Savvyio.Extensions.DependencyInjection, including singleton defaults and configured options registration.

Key APIs

AddRabbitMqCommandQueue registers the non-marker RabbitMqCommandQueue as a message queue for ICommand and stores the supplied RabbitMqCommandQueueOptions in the container. The implementation defaults to ServiceLifetime.Singleton, and the tests show the registration resolves through IPointToPointChannel<ICommand>, ISender<ICommand>, IReceiver<ICommand>, and Action<RabbitMqCommandQueueOptions>.

AddRabbitMqCommandQueue<TMarker> registers the marker-aware RabbitMqCommandQueue<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>.

AddRabbitMqEventBus registers the non-marker RabbitMqEventBus as the publish-subscribe channel for IIntegrationEvent and stores the supplied RabbitMqEventBusOptions. The tests verify that the same registration is available through IPublishSubscribeChannel<IIntegrationEvent>, IPublisher<IIntegrationEvent>, ISubscriber<IIntegrationEvent>, and Action<RabbitMqEventBusOptions>.

AddRabbitMqEventBus<TMarker> registers the marker-aware RabbitMqEventBus<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.

RabbitMqCommandQueue<TMarker> derives from Savvyio.Extensions.RabbitMQ.Commands.RabbitMqCommandQueue and implements IPointToPointChannel<ICommand, TMarker>. Its constructor keeps the lower-level IMarshaller and RabbitMqCommandQueueOptions<TMarker> contract while making the queue visible to the marker-based DI surface.

RabbitMqCommandQueueOptions<TMarker> inherits Savvyio.Extensions.RabbitMQ.Commands.RabbitMqCommandQueueOptions and adds IDependencyInjectionMarker<TMarker>. The wrapper does not introduce new properties, so queue setup still comes from the lower-level options such as QueueName, AmqpUrl, Persistent, AutoAcknowledge, Durable, Exclusive, and AutoDelete.

RabbitMqEventBus<TMarker> derives from Savvyio.Extensions.RabbitMQ.EventDriven.RabbitMqEventBus and implements IPublishSubscribeChannel<IIntegrationEvent, TMarker>. It is the marker-aware counterpart that lets DI resolve a RabbitMQ event bus through the marker-specific publish-subscribe interfaces.

RabbitMqEventBusOptions<TMarker> inherits Savvyio.Extensions.RabbitMQ.EventDriven.RabbitMqEventBusOptions and adds IDependencyInjectionMarker<TMarker>. Like the queue options wrapper, it keeps lower-level settings such as ExchangeName, AmqpUrl, and Persistent 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.RabbitMQ;
using Xunit;

namespace MyProject.Tests;

public class RabbitMqRegistrationTests : Test
{
    public RabbitMqRegistrationTests(ITestOutputHelper output) : base(output) { }

    [Fact]
    public void AddRabbitMqCommandQueue_ConfiguredMarker_RegistersServices()
    {
        var services = new ServiceCollection();
        services.AddRabbitMqCommandQueue<OrdersQueue>(options => options.QueueName = "orders.commands");

        TestOutput.WriteLine($"Registered services: {services.Count}");
        Assert.Contains(services, d => d.ServiceType.Name.Contains("IPointToPointChannel"));
        Assert.Contains(services, d => d.ServiceType.Name.Contains("ISender"));
        Assert.Contains(services, d => d.ServiceType.Name.Contains("IReceiver"));
    }
}

public readonly struct OrdersQueue { }

Use this pattern when you want to verify the RabbitMQ command-queue registrations before building a container. It matters because the package contributes the marker-specific channel service descriptors and typed option setup in one call.

Installation

dotnet add package Savvyio.Extensions.DependencyInjection.RabbitMQ

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 RabbitMQ-backed channels, especially when marker types separate multiple registrations in the same container. Register an IMarshaller before resolving these services, and configure QueueName for command queues or ExchangeName for event buses because the underlying RabbitMQ options validate those values. If you do not need Microsoft DI registration or the Savvy I/O channel abstractions, use Savvyio.Extensions.RabbitMQ or the RabbitMQ client APIs directly.

Family packages