Skip to content

The free mediator for .NET with the MediatR API

AnyMediator runs your requests, notifications and pipeline behaviors in-process. Apache-2.0, with no license key and no revenue limit.

dotnet add package AnyMediator

Try the migration

Pick a file from a MediatR project, or paste your own. The changes AnyMediator needs are applied as you type, in your browser.

Program.cs
−using MediatR;
+using AnyMediator;
 using MyApp.Users;
 
 var builder = WebApplication.CreateBuilder(args);
 
−builder.Services.AddMediatR(cfg =>
+builder.Services.AddAnyMediator(cfg =>
 {
−    cfg.LicenseKey = builder.Configuration["MediatR:Key"];
     cfg.RegisterServicesFromAssemblyContaining<GetUserHandler>();
     cfg.AddOpenBehavior(typeof(LoggingBehavior<,>));
 });
 
 var app = builder.Build();
 
 app.MapGet("/users/{id}", (int id, ISender sender) =>
     sender.Send(new GetUser(id)));
 
 app.Run();
Lines changed3

Behaviors, handlers, Send(object) and Publish work as in MediatR.

Read the full migration guide

Then swap the package: dotnet remove package MediatR and dotnet add package AnyMediator.

Starting fresh

First, install the package.

$ dotnet add package AnyMediator
info : Adding PackageReference for package 'AnyMediator' into project 'MyApp.csproj'.
info : PackageReference for package 'AnyMediator' version '2.0.0' added to file 'MyApp.csproj'.

Then, register it with your services.

builder.Services.AddAnyMediator(cfg =>
{
    cfg.RegisterServicesFromAssemblyContaining<Program>();
    cfg.AddOpenBehavior(typeof(LoggingBehavior<,>));
    cfg.AddOpenBehavior(typeof(ValidationBehavior<,>));
});
Added to the service collection
ISender, IPublisher, IMediatorthe mediator itself
IRequestHandler<GetUser, User>GetUserHandler, found by scanning
IPipelineBehavior<,>LoggingBehavior, then ValidationBehavior

Finally, send a request.

var user = await sender.Send(new GetUser(42));

What runs, in registration order

  1. LoggingBehavior before next()
  2. ValidationBehavior before next()
  3. GetUserHandler.Handle returns the user with Id 42
  4. ValidationBehavior after next()
  5. LoggingBehavior after next(), then the response goes back to the caller

What is in the box

Requests and streamsOne handler per request. Send returns its response; CreateStream yields items as they arrive.
Requests and streamsIRequest<TResponse>IRequestHandler<TRequest, TResponse>IStreamRequestHandler<TRequest, T>ISender.Send / CreateStream
NotificationsAny number of handlers per event, run one after another or in parallel.
NotificationsINotificationINotificationHandler<TNotification>IPublisher.Publish
Pipeline behaviorsMiddleware around every request, run in the order you register it.
Pipeline behaviorsIPipelineBehavior<TRequest, TResponse>IStreamPipelineBehavior<TRequest, T>RequestHandlerDelegate<TResponse>
Pre- and post-processorsLighter hooks that run just before and just after the handler.
Pre- and post-processorsIRequestPreProcessor<TRequest>IRequestPostProcessor<TRequest, TResponse>
Exception handlingRecover with a fallback response, or log and let the exception continue.
Exception handlingIRequestExceptionHandler<TRequest, TResponse, TException>IRequestExceptionAction<TRequest, TException>RequestExceptionHandlerState<TResponse>
Notification sinksSend the same notification to in-process handlers, a broker or a webhook.
Notification sinksINotificationSinkINotificationPublisherTaskWhenAllPublisher

Runs on.NET 8.NET 10ASP.NET CoreWorker servicesAzure FunctionsConsole apps

Install it, register it, and keep the handlers you already wrote.