Joselct.Outbox.MediatR 1.0.5

dotnet add package Joselct.Outbox.MediatR --version 1.0.5
                    
NuGet\Install-Package Joselct.Outbox.MediatR -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Joselct.Outbox.MediatR" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Joselct.Outbox.MediatR" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Joselct.Outbox.MediatR" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Joselct.Outbox.MediatR --version 1.0.5
                    
#r "nuget: Joselct.Outbox.MediatR, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Joselct.Outbox.MediatR@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Joselct.Outbox.MediatR&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Joselct.Outbox.MediatR&version=1.0.5
                    
Install as a Cake Tool

Joselct.Outbox.MediatR

MediatR dispatcher adapter for the Joselct.Outbox library.

This package connects the Outbox background processor to MediatR, so processed outbox messages are dispatched as INotification events to their corresponding INotificationHandler implementations.

Installation

dotnet add package Joselct.Outbox.MediatR

Requirements

  • Joselct.Outbox.EFCore
  • MediatR 14+

Setup

1. Register services

builder.Services
    .AddOutbox<AppDbContext>(builder.Configuration)
    .AddOutboxMediatR();

Make sure MediatR is also registered and your handlers are included in the assembly scan:

builder.Services.AddMediatR(cfg =>
    cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));

2. Define your events

Your domain events must implement INotification:

public record OrderCreatedEvent(Guid OrderId, string CustomerEmail) : INotification;

3. Implement notification handlers

public class OrderCreatedHandler : INotificationHandler<OrderCreatedEvent>
{
    private readonly IEmailService _emailService;

    public OrderCreatedHandler(IEmailService emailService)
    {
        _emailService = emailService;
    }

    public async Task Handle(OrderCreatedEvent notification, CancellationToken ct)
    {
        await _emailService.SendConfirmationAsync(notification.CustomerEmail, ct);
    }
}

4. Publish events from your handlers

public class CreateOrderHandler
{
    private readonly AppDbContext _db;
    private readonly IOutboxPublisher _outbox;

    public CreateOrderHandler(AppDbContext db, IOutboxPublisher outbox)
    {
        _db = db;
        _outbox = outbox;
    }

    public async Task HandleAsync(CreateOrderCommand command, CancellationToken ct)
    {
        var order = new Order { Id = Guid.NewGuid(), ... };
        _db.Orders.Add(order);

        await _outbox.PublishAsync(new OrderCreatedEvent(order.Id, command.Email), ct);

        await _db.SaveChangesAsync(ct); // atomic — order + outbox message
    }
}

How It Works

MediatROutboxDispatcher implements IOutboxDispatcher and bridges the outbox processor with MediatR:

public class MediatROutboxDispatcher : IOutboxDispatcher
{
    public async Task DispatchAsync(object @event, Type eventType, CancellationToken ct)
    {
        if (@event is INotification notification)
            await _publisher.Publish(notification, ct);
        else
            throw new InvalidOperationException(
                $"{eventType.Name} must implement INotification to use the MediatR dispatcher.");
    }
}

The full flow looks like this:

CreateOrderHandler
  └── IOutboxPublisher.PublishAsync(new OrderCreatedEvent(...))
        └── serialized + saved to outbox_messages (same transaction)

OutboxBackgroundService (every N seconds)
  └── OutboxProcessor reads pending messages
        └── MediatROutboxDispatcher.DispatchAsync(event, eventType)
              └── IPublisher.Publish(notification)
                    └── OrderCreatedHandler.Handle(notification)

Important: Events Must Implement INotification

If you publish an event that does not implement INotification, the dispatcher will throw an InvalidOperationException at dispatch time. Make sure all events you publish via IOutboxPublisher implement INotification when using this adapter.

// ✅ Correct
public record OrderCreatedEvent(Guid OrderId) : INotification;

// ❌ Will throw at dispatch time
public record OrderCreatedEvent(Guid OrderId);

Using a Different Dispatcher

If you prefer not to use MediatR, you can implement IOutboxDispatcher directly and skip this package entirely:

public class MyCustomDispatcher : IOutboxDispatcher
{
    public async Task DispatchAsync(object @event, Type eventType, CancellationToken ct)
    {
        // publish to RabbitMQ
    }
}

builder.Services
    .AddOutbox<AppDbContext>(builder.Configuration)
    .AddScoped<IOutboxDispatcher, MyCustomDispatcher>();

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 305 3/3/2026
1.0.4 120 3/3/2026
1.0.3 119 3/1/2026
1.0.2 120 2/28/2026
1.0.1 122 2/24/2026
1.0.0 119 2/24/2026