jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
327 stars 90 forks source link

Notification handlers are still registered twice in v9 #100

Closed maelmahdy86 closed 3 years ago

maelmahdy86 commented 3 years ago

I'm using mediatR v9 with .net 5 and I have this line in my startup class:

services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.StartsWith("MyCompany", StringComparison.OrdinalIgnoreCase)).ToArray());

Now, I've 3 notifications (EntityAdded, EntityUpdated, EntityDeleted) and one notification handler for the 3 of them

When I publish EntityAdded notification once, the handler gets called twice

After debugging, I found the handler is registered twice

maelmahdy86 commented 3 years ago

Figured it out :)

In my startup, I register the event handler to the IServicesCollection then call AddMediatR which re-registers the same handler again

I think you should check if the handler is already registered before registering it to avoid such issue

I solved it by calling AddMediatR before my dependencies registration