MediatR with ASP.NET Core DI

There´s a good tutorial by https://dotnetcoretutorials.com/. This's the example code for the correct installation and configuration of MediatR.

Installing MediatR

The first thing we need to do is install the MediatR nuget package. So from your package manager console run :

Install-Package MediatR

We also need to install a package that allows us to use the inbuilt IOC container in .NET Core to our advantage (We’ll see more of that shortly). So also install the following package :

Install-Package MediatR.Extensions.Microsoft.DependencyInjection

Finally we open up our startup.cs file. In our ConfigureServices method, we need to add in a call to register all of MediatR’s dependencies.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMediatR(Assembly.GetExecutingAssembly());
    //Other injected services. 
}

This is the link: https://dotnetcoretutorials.com/2019/04/30/the-mediator-pattern-part-3-mediatr-library/

I hope this helps.


As of July 2016, Jimmy Bogard, author of MediatR, has released a package to register MediatR, and Handlers, with the ASP.Net Core DI service (which is actually the interface IServiceCollection, implemented in Microsoft.Extensions.DependencyInjection and which is not restricted to use solely within ASP.Net Core).

MediatR.Extensions.Microsoft.DependencyInjection

Link to GitHub Project.

Link to NuGet Package information.

A blog post introducing the package and it's capabilities can be found here

Example registration copied directly from the (very short) blog post:

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();

  services.AddMediatR(typeof(Startup));
}

This package performs several functions to enable MediatR, including the required scanning of assemblies for Handlers:

You can either pass in the assemblies where your handlers are, or you can pass in Type objects from assemblies where those handlers reside. The extension will add the IMediator interface to your services, all handlers, and the correct delegate factories to load up handlers. Then in your controller, you can just use an IMediator dependency:

public class HomeController : Controller
{
  private readonly IMediator _mediator;

  public HomeController(IMediator mediator)
  {
    _mediator = mediator;
  }
  public IActionResult Index()
  {
    var pong = _mediator.Send(new Ping {Value = "Ping"});
    return View(pong);
  }
}

I got it working, my code:

public void ConfigureServices(IServiceCollection services)
{
      services.AddScoped<SingleInstanceFactory>(p => t => p.GetRequiredService(t));
      services.AddScoped<MultiInstanceFactory>(p => t => p.GetRequiredServices(t));
      services.Scan(scan => scan
              .FromAssembliesOf(typeof(IMediator), typeof(MyHandlerOne.Handler))
              .FromAssembliesOf(typeof(IMediator), typeof(MyHandlerTwo.Handler))
             .AddClasses()
             .AsImplementedInterfaces());
}

and I have a class that implements the GetRequiredService that MultiInstanceFactory need:

public static class GetServices
{
    public static IEnumerable<object> GetRequiredServices(this IServiceProvider provider, Type serviceType)
    {
        return (IEnumerable<object>)provider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(serviceType));
    }
}