MediatR.Extensions.RegisterGenericHandlers 1.0.1

dotnet add package MediatR.Extensions.RegisterGenericHandlers --version 1.0.1
NuGet\Install-Package MediatR.Extensions.RegisterGenericHandlers -Version 1.0.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="MediatR.Extensions.RegisterGenericHandlers" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MediatR.Extensions.RegisterGenericHandlers --version 1.0.1
#r "nuget: MediatR.Extensions.RegisterGenericHandlers, 1.0.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.
// Install MediatR.Extensions.RegisterGenericHandlers as a Cake Addin
#addin nuget:?package=MediatR.Extensions.RegisterGenericHandlers&version=1.0.1

// Install MediatR.Extensions.RegisterGenericHandlers as a Cake Tool
#tool nuget:?package=MediatR.Extensions.RegisterGenericHandlers&version=1.0.1

MediatR.Extensions.RegisterGenericHandlers

CI NuGet NuGet

Simple extension method for MediatR to register open generic request handlers.

Created as supplement to the famous MediatR library. (MediatR is the only Dependency).

Installing MediatR.Extensions.RegisterGenericHandlers

You should install MediatR.Extensions.RegisterGenericHandlers with NuGet:

Install-Package MediatR.Extensions.RegisterGenericHandlers

Or via the .NET Core command line interface:

dotnet add package MediatR.Extensions.RegisterGenericHandlers

Either commands, from Package Manager Console or .NET Core CLI, will download and install MediatR.Extensions.RegisterGenericHandlers and all required dependencies.

Registering with IServiceCollection

Extension method should be called on the service collection in your Program.cs file. I typically do it immediatlely after the MediatR registration.

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Startup>())
    .RegisterGenericHandlersFromAssemblyContaining<Startup>();

or with an IEnumerable<Assembly> of assemblies to scan:

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly))
    .RegisterGenericHandlers([typeof(Startup).Assembly]);

This registers open generic implementations for:

  • IRequestHandler<,> open implementations as transient
  • IRequestHandler<> open implementations as transient

Example Handler

Below is an example open generic request / handler implementation.

Some concrete class (Foo) that implements an interface (IFoo). This will be used as a type parameter for the example.

    public interface IFoo
    {
        string Bar { get; set; }        
    }

    public class Foo : IFoo
    {
        public string Bar { get; set; } = "Foo Bar";       
    }

Open request implementation that constrains the type parameter to IFoo


    public class GenericRequest<T> : IRequest<string>
        where T : class, IFoo
    {
        public T? Foo { get; set; }
    }

Open handler implementation for GenericRequest<T>


    public class GenericRequestHandler<T> : IRequestHandler<GenericRequest<T>, string?>
        where T : class, IFoo
    {
        public Task<string?> Handle(GenericRequest<T> request, CancellationToken cancellationToken)
        {
            request.Foo!.Bar = "Hello From Handler!!";

            return Task.FromResult(request.Foo?.Bar);
        }
    }

Usage from MediatR Send method.

   var request = new GenericRequest<Foo>
   {
       Foo = new()
   };
   var result = await _mediator.Send(request);

   Console.WriteLine(result); //outputs "Hello From Handler!!"
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.1 95 3/26/2024
1.0.0 83 3/26/2024
0.0.8 93 3/26/2024
0.0.7 91 3/26/2024