question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type implementing INotificationHandler and IRequestHandler is called more than once on Notification

See original GitHub issue

I have a type MySuperHandler that implements an INotificationHandler<SomethingHappened> and also an IRequestHandler<ShouldItHappen, true>.

The type is registered through MediatR.Extensions.Autofac.DependencyInjection (3.0.0) once at startup using builder.AddMediatR(typeof(Program).Assembly);

When I publish the Notification SomethingHappened using

await _mediator.Publish(new SomethingHappened());

the Handle method for SomethingHappened of MySuperHandler gets called twice.

Implementing multiple different INotificationHandler on a type is no problem. The Handle will only be called once.

Removing the interface IRequestHandler<ShouldItHappen,true> from the type, solves the problem and the method gets only called once.

Is this a bug within Mediatr? Is it not allow to implement an INoficationHandler and IRequestHandler on the same type?

public class MySuperHander : INotificationHandler<SomethingHappened>, IRequestHandler<ShouldItHappen,bool>    {

    public async Task Handle(SomethingHappened notification, CancellationToken cancellationToken)   {
        Console.WriteLine("It has happened");
    }

    public async Task<bool> Handle(ShouldItHappen request, CancellationToken cancellationToken)    {
        return true;
    }
}

public class ShouldItHappen : IRequest<bool>    {    }

public class SomethingHappened : INotification    {    }

This will print out “It has happened” twice. when calling await _mediator.Publish(new SomethingHappened());

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jehofcommented, Jul 12, 2019

I think you are right. Currently dumping autofac registrations and I think I saw MySuperHandler registered more than once for INotificationHandler. Let me check that

0reactions
lilasquaredcommented, Jul 15, 2019

I would say in general when keeping SOLID in mind you would want to isolate responsibility of the notification handlers to their own classes. Any shared functionality between them could be offloaded to an injected dependent service if necessary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MediatR: INotification handler is being called multiple times
Handle method is being called multiple times - I noticed it seems to be relative to number of command handlers registered in Startup.cs,...
Read more >
Diagnosing and Fixing MediatR Container Issues
I'd have to do that registration for each and every implementation to make sure the handler gets called.
Read more >
All you need to know about introducing the MediatR library ...
The class must implement INotificationHandler, while we need to ... If more than one class is to handle a request, then we need...
Read more >
CQRS and MediatR in ASP.NET Core
With these two classes, we create two handlers called EmailHandler and CacheInvalidationHandler that essentially do the same thing: Implement ...
Read more >
Using MediatR in ASPNET Core Apps | Blog
... and one or more handlers of type INotificationHandler . When you publish a notification, every handler is called in response.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found