Handle method are executed twice in INotificationHandler in a specific scenario.
See original GitHub issueI have a question regarding the behavior of the Handle method in INotificationHandler in a specific scenario.
I have an ApprovedEventHandler.cs class which is a NotificationHandler that sends email notifications and uses a generic type for reusability. Here is the code snippet:
public abstract class DomainEvent: INotification
{
protected DomainEvent()
{
DateOccurred = DateTimeOffset.UtcNow;
}
public bool IsPublished { get; set; }
public DateTimeOffset DateOccurred { get; protected set; }
}
public class ApprovedEvent<T>: DomainEvent
{
public ApprovedEvent(T entity)
{
Entity = entity;
}
public T Entity { get; }
}
public class DomainEventNotification<TDomainEvent> : INotification where TDomainEvent : DomainEvent
{
public DomainEventNotification(TDomainEvent domainEvent)
{
DomainEvent = domainEvent;
}
public TDomainEvent DomainEvent { get; }
}
public class ApprovedEventHandler<T> : INotificationHandler<DomainEventNotification<ApprovedEvent<T>>> where T : AuditableEntity
{
public ApprovedEventHandler()
{
}
public async Task Handle(DomainEventNotification<ApprovedEvent<T>> notification, CancellationToken cancellationToken)
{
var domainEvent = notification.DomainEvent;
}
}
The issue I am encountering is that when I exclude this ApprovedEventHandler.cs class from the project, all other NotificationHandlers are executed twice (subscribed twice) after compiling and adding the class back in.
I would appreciate it if someone could explain to me why this is happening. Although I have found a workaround for this problem, I am still curious to understand the underlying principle. Thank you.
public class AccessRequestCreatedEvent: DomainEvent
{
public AccessRequestCreatedEvent(AccessRequest item)
{
Item = item;
}
public AccessRequest Item { get; }
}
public class AccessRequestCreatedEventHandler: INotificationHandler<AccessRequestCreatedEvent>
{
public AccessRequestCreatedEventHandler(
)
{
}
public async Task Handle(AccessRequestCreatedEventnotification, CancellationToken cancellationToken)
{
var request = notification.Item;
}
}
Issue Analytics
- State:
- Created 6 months ago
- Reactions:3
- Comments:15 (2 by maintainers)
Top 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
As IntegrationEventHandler is registered twice. That means we leave ourselves open for that handler to get called twice. Not ideal! But explicit ...
Read more >MediatR
This package is useful in scenarios where your MediatR contracts are in a separate assembly/project from handlers. Example scenarios include:.
Read more >MediatR
I have a question regarding the behavior of the Handle method in INotificationHandler in a specific scenario. I have an ApprovedEventHandler.cs class which ......
Read more >Why Do We Need MediatR?
As you can see, this interface requires the implementation of a single Handle method that asynchronously performs the requested operation ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is the issue on dotnet side: https://github.com/dotnet/runtime/issues/87017
In this issue there are two MRs that fix that problem: https://github.com/dotnet/runtime/pull/80410 https://github.com/dotnet/runtime/pull/52035
My last PR for Microsoft DI was open for 5 years (literally) before merging so…good luck lol.