-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Duplicate] Notification Handler incorrectly executing twice #948
Comments
This might be a duplicate of #884 but I'm not sure if my scenario is the same. I ended up making the repository to isolate my issue as part of a work investigation task I was assigned. I thought I'd share it here as a new issue and link it to that existing one in case it helps. |
I'm pretty sure this issue explains the problem perfectly: But unfortunately, my project uses an older version of .NET Core so I'm hoping a MediatR update can provide a workaround for this bug so I only have to update MediatR. It sounds like this is being actively worked on according to the comment in the other MediatR issue. Feel free to close this issue if this is the case, but please feel free to use my repo for testing or investigating if it helps in anyway. Thanks all for the great work! |
I don't try to work around issues in the stock MS DI container. Instead you should investigate 3rd party containers (Lamar, Autofac etc.) |
I have created an ASP.Net Core Web API with tests to demonstrate and recreate the bug:
https://github.com/Mayron/Demo-NotificationService/tree/master
The README explains most of the issue in detail, but to summarise:
When any notification handler implementing the
INotificationHandler
interface exists in the codebase that makes use of a generic type parameter, then it causes all other notification handlers to incorrectly be executing twice for each mediator publish call.For example:
This shows how I'm publishing the events, but please view my git repository for the full code and README for more details:
This only seems to happen if the service provider's
ValidateOnBuild
configuration option is set totrue
to run a useful service dependency validation check during the build process (which our dev team rely on, but we have temporarily disabled it).I believe this is due to this validation step creating 2 service descriptors for each type of class that implements
INotificationHandler
instead of 1 (one being an array of that service, and 1 being the service itself) because the generic type constraint confuses the process.Perhaps the
AddMediatR
extension method could accommodate this validation check by tweaking the registered services to prevent this bug. Just a theory and it needs more investigation.The text was updated successfully, but these errors were encountered: