-
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
MediatR + Autofac NotificationHandler decorator pattern strange behaviour. #1063
Comments
Does the behavior change in any way if you downgrade MediatR to 12.2.0? |
Unfortunately no. I've tried with 12.2.0 and also other few random previous version of both MediatR and Autofac without success. |
Today I found an issue on Autofac repo that started me thinking that the problem might be on the autofac side. TL;DR; MediatR/src/MediatR/Wrappers/NotificationHandlerWrapper.cs Lines 24 to 26 in cac76be
MediatR is using an IServiceProvider to get all the handlers implementing INotificationHandler. In fact I verified that this line If instead I do not register the generic decorator, the above same line returns two distinct handlers as expected... |
This appears to be an autofac caused behavior, although I wouldn't call it a problem. Taking a look at your code, you're registering 3 types of INotificationHandler<IEventNotification> such that the final registration is asking for (without specificity) an instance of INotificationHandler<IEventNotification>. What you're observing is a result of autofac defaulting to the final registration of the requested type. If you swap (define first handler after second) the order in which your handlers are declared, your output will change and you will see 2 outputs from the first handler. You may want to look at autofac's keyed services fearure to help you define which instance you want injected in your decorator. |
Thanks for your reply though I cannot get how this is a desired (or expected) behaviour... However if the decorator is registered the decorated handler is not resolved correctly and the last registered instance is returned every time inside the decorator constructor. |
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days. |
This issue was closed because it has been stalled for 14 days with no activity. |
Hi,
I'm having a very strange issue with decorator pattern using MediatR (12.4.0) and Autofac (8.0.0) but I really cannot understand wether the problem is concerning Autofac, MediatR or (more probably) my code...
Basically I have a custom wrapper around INotification:
that is handled by this interface
My application will (very simplified) raise events.
These events (whose implementations are known) are handled by some handlers.
In some cases there are multiple handlers per event.
Now my code is working as expected until I introduce a decorator for the event handlers like this one
At this point, for a given event, the decorator is applied only to one of the declared handlers and the handlers itself is called multiple times!
This is somewhat difficult to explain, so I crafted a fully functional but yet simple example on dotnetfiddle:
https://dotnetfiddle.net/5sIGzj
As you can see, only the SecondEventNotificationHandler is called (and decorated) twice.
If the line 155
builder.RegisterGenericDecorator(typeof(CustomNotificationHandlerDecorator<>),typeof(INotificationHandler<>));
is commented out, the two event handlers are called as expected, (but the decorator is obviously not called).I am struggling with this for several days and I really cannot get my head around...
Thanks for the support and for your amazing work!
The text was updated successfully, but these errors were encountered: