4

Does anyone know how to implement MediatR in a console application, to call a handler function using the _mediatr.Send(obj) syntax. I'm using the .Net 6 framework. Thanks for the help.

bflow1
  • 49
  • 1
  • 9

1 Answers1

12

First, you must install these packages:

  1. Microsoft.Extensions.DependencyInjection
  2. MediatR
  3. MediatR.Extensions.Microsoft.DependencyInjection

Then you can get IMediator from DI and use it.

using MediatR;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

var serviceCollection = new ServiceCollection()
    .AddMediatR(Assembly.GetExecutingAssembly())
    .BuildServiceProvider();

var mediator = serviceCollection.GetRequiredService<IMediator>();

//mediator.Send(new Command());
Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41