Medianiz 2.2.1

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

Medianiz

NuGet Version .NET Standard License

A lightweight implementation of the mediator pattern for .NET applications, designed to simplify in-process messaging.

Release Versions

  • 2.2.1: Fixed handler discovery for services.AddMedianiz(config => config.RegisterHandlersFromAssembly(typeof(Program))) in WebAPI/DDD solutions. Medianiz now scans the non-framework assemblies referenced by the marker assembly, so handlers from layers such as Application are registered automatically.
  • 2.2.0: Some libraries were updated and RegisterHandlersFromAssembly was implemented.
  • 2.1.0: Some libraries were updated and Medianiz has implementation of Pipeline Behaviors.

Pipeline Behaviors

Medianiz supports Pipeline Behaviors, allowing cross-cutting logic to be executed before and/or after a request handler, following the same model used by MediatR’s IPipelineBehavior.

This feature is useful for implementing concerns such as:

  • Logging
  • Validation
  • Transactions
  • Metrics
  • Exception handling

Concept

A Pipeline Behavior wraps the execution of a request handler and can:

  • Execute code before the handler
  • Execute code after the handler
  • Stop execution by throwing exceptions
  • Decorate or modify the response

Execution is performed as a chained pipeline.


IPipelineBehavior Interface

public delegate Task<TResponse> RequestHandlerDelegate<TResponse>();

public interface IPipelineBehavior<in TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    Task<TResponse> Handle(
        TRequest request,
        RequestHandlerDelegate<TResponse> next,
        CancellationToken cancellationToken
    );
}

DOCUMENTATION

Key Features

  • Simple request/response handling
  • Event publishing to multiple handlers
  • First-class async/await support
  • CancellationToken integration
  • Minimal dependencies
  • Works with any DI container

Installation

dotnet add package Medianiz

Basic Usage

1. Configure Services

// In Startup.cs or Program.cs
builder.Services.AddMedianiz(typeof(MyCommandHandler));

For multi-project solutions:

// Reference assemblies by project name
builder.Services.AddMedianizFromAssemblies("MyApp.Application");

2. Define Commands and Handlers

Command:

public record CreateUserCommand(string Email) : IRequest<Guid>;

Handler:

public class CreateUserHandler : IRequestHandler<CreateUserCommand, Guid>
{
    public Task<Guid> Handle(CreateUserCommand request, CancellationToken ct)
    {
        // Business logic here
        return Task.FromResult(Guid.NewGuid());
    }
}

3. Define Events and Handlers

Event:

public record UserCreatedEvent(Guid UserId) : INotification;

Handler:

public class UserCreatedHandler : INotificationHandler<UserCreatedEvent>
{
    public Task Handle(UserCreatedEvent notification, CancellationToken ct)
    {
        // React to the event
        return Task.CompletedTask;
    }
}

4. Use in ASP.NET Core

Controller Example:

[ApiController]
[Route("users")]
public class UsersController : ControllerBase
{
    private readonly IMedianiz _mediator;

    public UsersController(IMedianiz mediator)
    {
        _mediator = mediator;
    }

    [HttpPost]
    public async Task<IActionResult> CreateUser([FromBody] CreateUserCommand command)
    {
        var userId = await _mediator.Send(command);
        return CreatedAtAction(nameof(GetUser), new { id = userId });
    }
}

Advanced Scenarios

Domain Events

public class Order : Entity
{
    public void Complete()
    {
        AddDomainEvent(new OrderCompletedEvent(Id));
    }
}

// Publish events after saving
var order = await _repository.GetOrderAsync();
order.Complete();
await _mediator.PublishDomainEvents(order);

Working with Multiple Assemblies

// Create a marker interface in each project
public interface IApplicationMarker { }

// Then register all needed assemblies
builder.Services.AddMedianiz(
    typeof(Application.Marker),
    typeof(Domain.Marker),
    typeof(Infrastructure.Marker));

Configuration Options

Register Specific Handlers Only

services.AddMedianiz(config =>
{
    config.RegisterHandlersFromAssembly(typeof(Program));
    config.RegisterHandler<SpecialHandler>(); // Manual registration
});

When you use a Web API marker such as typeof(Program), Medianiz also scans the non-framework assemblies referenced by that project so handlers in your Application layer are discovered automatically.

Example in a DDD solution:

// Civika.API
services.AddMedianiz(config =>
{
    config.RegisterHandlersFromAssembly(typeof(Program));
});

// QueryDesempenhoMunicipioCommandHandler can live in Civika.Application
// and will still be registered automatically if Civika.API references it.

Lifecycle Management

// Change handler lifetime (default is Transient)
services.AddMedianiz(typeof(Startup))
    .WithHandlerLifetime(ServiceLifetime.Scoped);

Performance Tips

  1. Assembly Filtering: Only scan necessary assemblies
  2. Marker Interfaces: Use them to limit handler discovery
  3. Lifetime Management: Use Scoped for DB-heavy handlers

Contributing

Pull requests are welcome. For major changes, please open an issue first.

License

GPL 3.0 - See LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.2.1 112 8/7/2026
2.2.0 104 8/4/2026
2.1.0 141 1/16/2026
2.0.0 430 11/18/2025
1.0.2 214 6/15/2025
1.0.1 192 6/14/2025
1.0.0-beta 245 4/14/2025