Minicon.Mediator 3.0.0

dotnet add package Minicon.Mediator --version 3.0.0
                    
NuGet\Install-Package Minicon.Mediator -Version 3.0.0
                    
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="Minicon.Mediator" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Minicon.Mediator" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Minicon.Mediator" />
                    
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 Minicon.Mediator --version 3.0.0
                    
#r "nuget: Minicon.Mediator, 3.0.0"
                    
#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 Minicon.Mediator@3.0.0
                    
#: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=Minicon.Mediator&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Minicon.Mediator&version=3.0.0
                    
Install as a Cake Tool

Minicon.Mediator

A lightweight mediator for .NET — request/response, streaming, notifications and pipeline behaviors with exactly one runtime dependency (Microsoft.Extensions.DependencyInjection.Abstractions).

The type names follow MediatR and Mediator (martinothamar), so code from either migrates with little more than a using swap. See Migrating.

Independent reimplementation by minicon eG. Not affiliated with or endorsed by the MediatR or Mediator projects. "MediatR" is a trademark of its respective owner; the familiar type names (IRequest, INotification, …) are kept for an easy migration, but this package lives in its own Minicon.Mediator namespace.

3.0.0 is a breaking change. IRequestHandler<,>.Handle now returns ValueTask<TResponse> instead of Task<TResponse>. Handlers that return Task implement ITaskRequestHandler<,> instead — one line per handler, bodies unchanged. Everything else (Send, Publish, behaviors, streaming) is untouched.

Install

dotnet add package Minicon.Mediator

Targets net10.0.

Usage

using Minicon.Mediator;
using Microsoft.Extensions.DependencyInjection;

// 1) Register — scans the given assembly for handlers
services.AddMediator(cfg => cfg.RegisterServicesFromAssemblyContaining<Ping>());

// 2) Request / Response
public record Ping(string Message) : IRequest<string>;

public sealed class PingHandler : IRequestHandler<Ping, string>
{
    public ValueTask<string> Handle(Ping request, CancellationToken ct)
        => ValueTask.FromResult($"Pong: {request.Message}");
}

// …or, to keep a Task-returning handler:
public sealed class PingTaskHandler : ITaskRequestHandler<Ping, string>
{
    public Task<string> Handle(Ping request, CancellationToken ct)
        => Task.FromResult($"Pong: {request.Message}");
}

var answer = await mediator.Send(new Ping("hi"));   // "Pong: hi" — Send returns Task<T>

// 3) Notifications (fan-out to all handlers)
public record UserRegistered(string Email) : INotification;

public sealed class SendWelcome : INotificationHandler<UserRegistered>
{
    public Task Handle(UserRegistered n, CancellationToken ct) => /* ... */ Task.CompletedTask;
}

await mediator.Publish(new UserRegistered("a@b.de"));

// 4) Pipeline behaviors (logging, validation, transactions, …)
public sealed class LoggingBehavior<TReq, TRes> : IPipelineBehavior<TReq, TRes>
    where TReq : notnull
{
    public async Task<TRes> Handle(TReq req, RequestHandlerDelegate<TRes> next, CancellationToken ct)
    {
        // before
        var res = await next();
        // after
        return res;
    }
}

Streaming

For handlers that produce many responses over time, use IStreamRequest<T> and CreateStream. Elements are forwarded as the handler yields them — nothing is buffered, so the consumer can break out early or cancel at any point.

public record Tail(string File) : IStreamRequest<string>;

public sealed class TailHandler : IStreamRequestHandler<Tail, string>
{
    public async IAsyncEnumerable<string> Handle(Tail request, [EnumeratorCancellation] CancellationToken ct)
    {
        await foreach (var line in File.ReadLinesAsync(request.File, ct))
        {
            yield return line;
        }
    }
}

await foreach (var line in mediator.CreateStream(new Tail("app.log"), ct))
{
    Console.WriteLine(line);
}

Streams have their own behavior interface, IStreamPipelineBehavior<TReq, TRes>, which wraps the handler the same way IPipelineBehavior<,> does for Send:

public sealed class CountingBehavior<TReq, TRes> : IStreamPipelineBehavior<TReq, TRes>
    where TReq : notnull
{
    public async IAsyncEnumerable<TRes> Handle(
        TReq req, StreamHandlerDelegate<TRes> next, [EnumeratorCancellation] CancellationToken ct)
    {
        var count = 0;
        await foreach (var item in next(ct).WithCancellation(ct))
        {
            count++;
            yield return item;
        }
        // after — count is complete here
    }
}

Handlers are discovered by the same assembly scan as IRequestHandler<,>; stream behaviors are registered manually (services.AddTransient<IStreamPipelineBehavior<…>, …>()).

Two behavior shapes

IPipelineBehavior<TRequest, TResponse> accepts either of two Handle overloads — implement whichever one your existing code uses:

// MediatR shape — Task, next() / next(ct)
Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken ct);

// Mediator (martinothamar) shape — ValueTask, next(request, ct)
ValueTask<TResponse> Handle(TRequest request, CancellationToken ct, MessageHandlerDelegate<TRequest, TResponse> next);

The pipeline invokes the first one, so that is the allocation-free path; the second is reached through a default interface implementation costing one closure and a ValueTask/Task conversion per call. Implement exactly one — a behavior that implements neither compiles but recurses endlessly at runtime.

Note that the request handed to next(request, ct) is accepted for signature compatibility only: the inner stages always run against the request the pipeline started with.

Migrating from Mediator (martinothamar)

  • using Mediator; → using Minicon.Mediator;
  • services.AddMediator() (source-generated) → services.AddMediator(cfg => cfg.RegisterServicesFromAssemblyContaining<T>())
  • IRequest<T>, IPipelineBehavior<,>, MessageHandlerDelegate<,> keep their names; behaviors in the (request, cancellationToken, next) / ValueTask shape compile unchanged (see above).
  • cfg.ServiceLifetime works as an alias of cfg.Lifetime. Note the default differs: Transient here (MediatR behavior) versus Singleton in Mediator — set it explicitly if you relied on the latter.
  • IRequestHandler<,> returns ValueTask<TResponse> as it does in Mediator, so handlers compile unchanged.

Not carried over: Send returns Task<T>, not ValueTask<T> (await works either way, assigning to a ValueTask<T> does not). IMessage, ICommand/IQuery and their handler interfaces have no equivalent — use IRequest/IRequest<T>. Notifications always fan out sequentially; there is no INotificationPublisher to swap in. INotificationHandler and IStreamRequestHandler keep their MediatR shapes (Task / IAsyncEnumerable).

Migrating from MediatR

Four replacements:

  • using MediatR; → using Minicon.Mediator;
  • services.AddMediatR(...) → services.AddMediator(...)
  • MediatRServiceConfiguration → MediatorServiceConfiguration
  • On request handlers: IRequestHandler<,> → ITaskRequestHandler<,>, keeping the Task<TResponse> Handle(...) body as is. (Or switch the return type to ValueTask<TResponse> and stay on IRequestHandler<,>.)

All other type names (IRequest, IRequest<T>, IStreamRequest<T>, INotification, IStreamRequestHandler<,>, INotificationHandler<>, IPipelineBehavior<,>, IStreamPipelineBehavior<,>, ISender, IPublisher, IMediator, Unit) are unchanged — including notification handlers, which keep returning Task.

License

MIT © minicon eG

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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
3.0.0 130 8/21/2026
2.3.0 111 8/21/2026
2.2.0 116 8/21/2026
2.1.0 110 8/17/2026
2.0.0 135 8/17/2026
1.0.0 138 6/8/2026