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.

Handle method are executed twice in INotificationHandler in a specific scenario.

See original GitHub issue

I 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:open
  • Created 6 months ago
  • Reactions:3
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
MichaelHochrieglcommented, Jul 13, 2023

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

2reactions
jbogardcommented, Jun 1, 2023

My last PR for Microsoft DI was open for 5 years (literally) before merging so…good luck lol.

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
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 >

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