jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
11.1k stars 1.18k forks source link

When using the publish mode(asp.net core webapi),each Handler being executed twice #389

Closed swpudp closed 5 years ago

swpudp commented 5 years ago

QQ截图20190403095319

QQ截图20190403100007

QQ截图20190403095605

syska commented 5 years ago

@swpudp

Can you recreate this issue in a Console app for .NET Core 2.x?

What IOC container are you using here? What version of Mediatr?

swpudp commented 5 years ago

@syska The IOC container is Autofac of version 4.9.2,MediatR version is 6.0.0

RDalziel commented 5 years ago

I think i'm getting the same (From a console app) - haven't confirmed whether it's specific to Autofac yet!

RDalziel commented 5 years ago

I think i'm getting the same (From a console app) - haven't confirmed whether it's specific to Autofac yet!

Odd coincidence we got the same issue at roughly the same time. But my problem was daft - i had called AddMediator accidently which caused 2 instances of the notification handler to be in the container which results in them both being called.

remcoros commented 5 years ago

Looks like a dependency registration issue. This can happen when you do scanning for types and (by accident) add multiple instances of the same handler to the container.

Without sample code it's hard to tell, but double check if you don't do any duplicate registrations.

you could check this quickly by resolving handlers manually. E.g.

var handlers = container.Resolve<INotificationHandler<TestNotification>>(); // use whatever method your container uses for resolving.

check 'handlers', it probably has double the amount of instances you expect.