Mecesoft.MediatR 1.0.3

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

📦 Mecesoft.MediatR

A lightweight, high-performance MediatR implementation for .NET 9.0, designed with simplicity and efficiency in mind.


✨ Features

  • � High Performance: Optimized reflection with caching for minimal runtime overhead.
  • 🛡️ Scope Safe: Correctly handles dependency injection scopes, ensuring transaction boundaries and scoped services (like DbContext) work as expected.
  • 🧩 Modular: Supports IRequest, IRequestHandler, INotification, and IPipelineBehavior.
  • � Safe Scanning: Robust assembly scanning that gracefully handles missing dependencies.
  • ✅ Pipeline Support: Easily add cross-cutting concerns like Validation, Logging, or Caching.

🧱 Installation

dotnet add package Mecesoft.MediatR

🔧 Usage

1. Register Services

In your Program.cs or startup configuration:

using Mecesoft.MediatR;

builder.Services.AddMediatR(cfg =>
{
    // Register handlers from the assembly containing 'Program'
    cfg.AddRegisterAssemblies(typeof(Program).Assembly);
    
    // Register pipeline behaviors (optional)
    cfg.AddOpenBehavior(typeof(ValidationBehavior<>));
    cfg.AddOpenBehavior(typeof(LoggingBehavior<>));
});
2. Create Request & Handler
// Define a Request (Command/Query)
public record GetUserByIdQuery(Guid Id) : IRequest<UserDto>;

// Implement the Handler
public class GetUserByIdHandler : IRequestHandler<GetUserByIdQuery, UserDto>
{
    private readonly IUserRepository _repository;

    public GetUserByIdHandler(IUserRepository repository)
    {
        _repository = repository;
    }

    public async Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
    {
        var user = await _repository.GetByIdAsync(request.Id);
        return user.ToDto();
    }
}
3. Send Request

Inject ISender into your Controllers or Services:

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    private readonly ISender _sender;

    public UsersController(ISender sender)
    {
        _sender = sender;
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> Get(Guid id)
    {
        var result = await _sender.Send(new GetUserByIdQuery(id));
        return Ok(result);
    }
}

⚠️ Important: Middleware & Singleton Services

If you are using ISender inside a Middleware or any Singleton service, you MUST NOT inject it via the constructor. Instead, inject it into the method (Method Injection) to ensure it uses the correct scope.

❌ Wrong (Captive Dependency):

public class MyMiddleware
{
    private readonly ISender _sender;
    public MyMiddleware(RequestDelegate next, ISender sender) // Error!
    {
        _sender = sender;
    }
}

✅ Correct (Method Injection):

public class MyMiddleware
{
    private readonly RequestDelegate _next;
    public MyMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context, ISender sender) // Correct!
    {
        await sender.Send(new MyCommand());
        await _next(context);
    }
}

🏷 License

MIT License – © Mecesoft

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.3 1,159 11/27/2025
1.0.2 177 11/27/2025
1.0.1 1,455 8/3/2025
1.0.0 111 8/3/2025

Security Update