-
-
Notifications
You must be signed in to change notification settings - Fork 235
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
Multiple nested decorators when multiple services implement same generic interface #125
Comments
Hey @trapezoide! 👋🏻 Thanks for this awesome issue with all the detailed information! I've managed to reproduce your scenario and it does indeed look like a bug. I've submitted #126 and will merge and put out a new release as soon as the CI finishes. |
Pushed v3.2.2 to NuGet. Thanks again! Let me know how if it fixes your issue 😄 |
It worked like a charm! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First of all, thanks for sharing this library with all of us. It's pretty awesome!
I don't know if this is an issue or not, but I'm having unexpected results when I use the same decorator to decorate multiple (n) services that implement the same generic interface. It seems like the decorator gets called (n) times when I call one of the services instead of being called just once.
Let me explain...
I have three interfaces:
I have MyEvent (class) that implements IEvent and 3 MyEventXHandler services that implement IEventHandler to handle the event:
I have one decorator to be used on all of my services (these 3 and any other service I create in the future). Notice that future services will implement IEventHandler, but TEvent may or may not necessarily be of MyEvent type):
I add the three services to IServiceCollection by scanning my assemblies:
Then add the decorator:
Services.TryDecorate(typeof(IEventHandler<>), typeof(MyEventHandlerDecorator<>));
When I call any of my services the decorator gets called three times in a row before getting into my service. It doesn't matter which service I call. I am expecting the decorator to be called just once, and then my service.
I went through Scrutor's code and believe I found out why it is behaving this way. Inside ServiceCollectionExtensions.Decoration.cs file there is a method:
In the first line of code, where 'arguments' is being set, the following returns 3 items instead of one.
.Select(x => x.ServiceType.GenericTypeArguments)
The 3 items are of the same type, but when compared are not equal. I tried to use .Distinct() right after the select, but 'arguments' still contained the 3 same items. So what I did instead was to create my own DistinctByType() extension method to compare the items returned by GenericTypeArguments and return only the distinct types.
and
This worked for me, but I don't know if it'll break anything else.
Please advise.
Thanks
The text was updated successfully, but these errors were encountered: