NativeMediator.SourceGenerator.DependencyInjection 1.0.3

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

NativeMediator.SourceGenerator.DependencyInjection

NuGet

AOT-first Source Generator for MediatR Handler Registration - Automatically registers all IRequestHandler<,> implementations without assembly scanning.

Features

  • 100% Native AOT Compatible - No reflection, no assembly scanning
  • Compile-time Registration - All handlers discovered at build time
  • Seamless MediatR Integration - Works with existing MediatR patterns
  • IIncrementalGenerator - Fast, incremental builds

Installation

dotnet add package NativeMediator.SourceGenerator.DependencyInjection
dotnet add package MediatR

Usage

1. Create your handlers as usual

using MediatR;

public record GetUserQuery(int Id) : IRequest<User>;

public class GetUserHandler : IRequestHandler<GetUserQuery, User>
{
    public Task<User> Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        return Task.FromResult(new User { Id = request.Id, Name = "John Doe" });
    }
}

2. Register with generated method

using Microsoft.Extensions.DependencyInjection;
using NativeMediator.SourceGenerator.DependencyInjection.Generated;

var services = new ServiceCollection();
services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Program>());

// Generated method - registers all IRequestHandler implementations
services.AddNativeMediatorHandlers();

Generated Code

The generator discovers all IRequestHandler<TRequest, TResponse> implementations and generates:

// Auto-generated
namespace NativeMediator.SourceGenerator.DependencyInjection.Generated
{
    public static class NativeMediatorServiceCollectionExtensions
    {
        public static IServiceCollection AddNativeMediatorHandlers(this IServiceCollection services)
        {
            // Scoped by default, matching MediatR conventions
            services.AddScoped<IRequestHandler<GetUserQuery, User>, GetUserHandler>();
            services.AddScoped<IRequestHandler<CreateOrderCommand, OrderResult>, CreateOrderHandler>();
            // ... all discovered handlers
            return services;
        }
    }
}

Handler Discovery

The generator automatically finds:

  • Classes implementing IRequestHandler<TRequest, TResponse>
  • Non-abstract, non-generic classes only
  • Both sync and async handlers

Diagnostics

Code Description
MED001 Handler must be a non-abstract class
MED002 Handler must have a public constructor

Requirements

  • .NET 10 SDK
  • C# 13+
  • MediatR package (v12+)

Why Use This?

MediatR's default registration uses Assembly.GetTypes() which is incompatible with Native AOT. This generator eliminates that by discovering handlers at compile-time and generating explicit registrations.

License

MIT

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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 71 2/11/2026
1.0.2 72 2/10/2026
1.0.1 74 2/10/2026
1.0.0 73 2/10/2026