Questions tagged [mediatr]

MediatR is a simple, open source mediator implementation in .NET.

MediatR is a simple, open source, mediator implementation in .NET

A low-ambition library trying to solve a simple problem - decoupling the in-proc sending of messages from handling messages. Cross-platform, supporting .NET 4.5, .NET Standard 1.3, and .NET Standard 2.0.

Supports request/response, commands, queries, notifications and events, synchronous and async with intelligent dispatching via C# generic variance.

See https://github.com/jbogard/MediatR/wiki for more details.

507 questions
47
votes
4 answers

IRequestHandler return void

Please see the code below: public class CreatePersonHandler : IRequestHandler { public async Task Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return true; } } It…
w0051977
  • 15,099
  • 32
  • 152
  • 329
46
votes
17 answers

ASP.NET Core MediatR error: Register your handlers with the container

I have a .NET Core app where I use the .AddMediatR extension to register the assembly for my commands and handlers following a CQRS approach. In ConfigureServices in Startup.cs i have used the extension method from the official package…
Mike Hawkins
  • 2,144
  • 5
  • 24
  • 42
37
votes
3 answers

Add validation to a MediatR behavior pipeline?

I'm using ASP.NET Core, the built-in container, and MediatR 3 which supports "behavior" pipelines: public class MyRequest : IRequest { // ... } public class MyRequestHandler : IRequestHandler { public string…
grokky
  • 8,537
  • 20
  • 62
  • 96
37
votes
6 answers

MediatR with ASP.NET Core DI

I'm playing around with the new ASP.NET Core and are currently creating a API that I want to call from a JavaScript frontend. I want to use the mediator pattern to reduce the coupling, and I have found the Library MediatR from Jimmy Bogard. My…
Nyegaard
  • 1,309
  • 3
  • 16
  • 24
32
votes
2 answers

MediatR publish and MediatR send

I have tried the CQRS pattern using MediatR and am loving the clean state in which applications am working on are transforming. In all the examples i have seen and used, I always do await Mediator.Send(command); It's been same for the queries as…
Tonto
  • 2,900
  • 3
  • 25
  • 34
29
votes
2 answers

How to instantiate Mediatr as part of a Unit Test?

I am trying to build an xUnit Test project for an MVC Core 2.2 Application that is based on the CQRS/ES pattern. I utilize MediatR as part of my CQRS/ES pattern in the MVC application. In one of my commands which I would like to test, I inject…
Roland
  • 437
  • 1
  • 4
  • 7
27
votes
2 answers

MediatR IPipelineBehavior errors as The type 'TRequest' cannot be used as type parameter 'TRequest' in the generic type or method

I'm using MediatR to do Request - Response logging in my application using IPipelineBehavior Code Sample: internal sealed class AppLoggingBehavior : IPipelineBehavior { private…
fingers10
  • 6,675
  • 10
  • 49
  • 87
22
votes
1 answer

Mocking MediatR 3 with Moq

We've recently started using MediatR to allow us to de-clutter controller actions as we re-factor a large customer facing portal and convert it all to C#. As part of this we are increasing our unit test coverage as well, but I've hit a problem when…
Steve Pettifer
  • 1,975
  • 1
  • 19
  • 34
20
votes
6 answers

the program is not able to find handler for MediatR query ASP.Net Core

I'm using ASP.Net Core 2.2 and MediatR framework/library for query objects. When I run the program i face to this exception: InvalidOperationException: Handler was not found for request of type …
Arian Shahalami
  • 1,369
  • 2
  • 13
  • 25
19
votes
4 answers

Could not load type 'MediatR.ServiceFactory'

I'm creating an Api and I am also using mediatR, I have created my commands, queries, handlers, but when I try to launch my Api I get this error message in my program class: Could not load type 'MediatR.ServiceFactory' from assembly 'MediatR,…
19
votes
1 answer

Mediatr Scope problems

I am using Mediatr to handle messages from a queue. I can get a simple example to work. However I have run into problems when I try to inject an object into my handler public class MessageCommandHandler : IRequestHandler { …
18
votes
2 answers

Add a generic handler for Send and Publish methods of the MediatR library in asp .net core

I use the CQS pattern in my asp.net core project. Let's start with an example to better explain what I want to achieve. I created a command: public class EmptyCommand : INotification{} The command handler: public class EmptyCommandHandler :…
GoldenAge
  • 2,918
  • 5
  • 25
  • 63
17
votes
3 answers

Replacing service layer with MediatR - is it worth to do it?

Do you think it might be reasonable to replace my service layer or service classes with MediatR? For example, my service classes look like this: public interface IEntityService where TEntityDto : class, IDto { Task
Konrad
  • 6,385
  • 12
  • 53
  • 96
16
votes
1 answer

Adding MediatR service

I want to add MediatR to my services. Here is the code: public class Program { public static async Task Main(string[] args) { var builder = WebApplication.CreateBuilder(args); …
MB_18
  • 1,620
  • 23
  • 37
16
votes
1 answer

Is it OK to have one handler call another when using MediatR?

Or is that considered bad practice or something? I have one notification triggers 4-5 handlers, which in turn call database to retrieve data. Each those calls can also be called separately, so they are request/handler themselves. Thanks.
Whoever
  • 1,295
  • 1
  • 14
  • 21
1
2 3
33 34