-
-
Notifications
You must be signed in to change notification settings - Fork 230
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
System.NotSupportedException
Thrown in 4.2.0 Using Decorate
#175
Comments
Hi, I had a look at this today. This is an issue between Scrutor and LightInject.Microsoft.DependencyInjection. The issue is around where LightInject.Microsoft.DependencyInjection is converting the MS registrations into LightInject registrations. It is attempting to create a factory by using reflection to create a generic method at runtime. This is using the reflection method MakeGenericMethod. This method is failing because it expects the passed types to be a RuntimeType. However, Scrutor is passing the DecoratedType which inherits from Type. This results in it returning an Emit based MethodBuilderInstantiation and the subsequent exception when calling Invoke. DecoratedType cannot inherit from RuntimeType as it is a private .Net runtime type. I created a test which recreates this scenario, it is here I am surprised by the number of dependency injection frameworks in use, and the large variances between them. The newly introduced DecoratedType hack is causing this issue and another. However for this issue I cannot see any way to fix this whilst keeping the DecoratedType. Given this issue and the other open issue, it might be worth reverting to the previous decoration strategy for Scrutor. Whilst it would be disappointing to lose the new added feature around #91. I do have a couple of different ideas to look into in the future which do not use this type of DecoratedType hack. |
Thank you for your investigation and explanation @DanHarltey. Am I to understand correctly that this is due to a new feature to ensure Dispose is called on all decorated components resolved via Scrutor? If it helps, I always call Ideally, there should be a way to configure this feature so that you can enable it if needed. I would suggest a new method that allows you to opt-in & enable it. This way you preserve both the existing/previous functionality as well as preserve the new feature so everyone wins. :) |
I'm actually leaning towards calling this a bug in LightInject.
From When is a Type not a Type? The version with the custom type has been out for a while and no one else has screamed, so it seems to be a bit of a fringe bug. Maybe we can get them to patch it on their side? Or send a PR? |
Let's bring in @seesharper and see if they can help out here. 😀 |
Created a PR at seesharper/LightInject.Microsoft.DependencyInjection#195. |
Thank you very much @khellang !!! 🙏🙏🙏 |
LightInject.Microsoft.DependencyInjection 3.4.2 is now available on NuGet with these changes. Again, thank you all for contributing 👍❤️ |
Amazing. Thanks @seesharper! 🙏😘 |
Sorry to be a downer here @khellang / @seesharper but I upgraded to the latest versions all around and I am still getting the error in the provided solution above. Is there something I am overlooking here? |
Hmm. @Mike-E-angelo is that the exact same exception as before? 🤔 |
Good question, @khellang. Here is using
And here is using
They appear to be the same, although I could be doing something wrong? Any verification on your end would be greatly appreciated. |
@Mike-E-angelo Could you pull in LightInject.Microsoft.DependencyInjection 3.4.2 explicitly and see if it works then? |
I think I see what is wrong. LI.MI.Hosting pulls in 3.4.1 which I at 12PM last night thought was the latest version.😃 I'll update LI.MI.Hosting to pull in 3.4.2 which contains the fix from @khellang 👍 |
@Mike-E-angelo LIghtInject.Microsoft.Hosting 1.3.2 is now out on NuGet. Could you try that? 😃 |
Thanks again @seesharper! 😁 |
I appreciate both of you being super responsive and engaging here. Thank you! The new version does seem to solve the problem captured in the solution, but now it appears it has moved onto a new one. 😁 When I open
Please let me know if you are able to spot the problem here or if I need to track this down for you and add a new test to that solution. |
Yeah, it's basically the same problem. I wonder what happens if we just use the The other alternative is to go through LightInject and call |
The quick fix would probably be as @khellang is suggesting here. Make sure that we use The long term solution would be to also fix this in LightInject 👍 |
I was afraid you were going to ask that 😅 Not at the moment but I will try to get something for you here today. This is an especially tricky bugger because for whatever reason Visual Studio optimizes all type data and it's really difficult to know which registration it's failing on. I think I had mentioned there are over 1200 registrations in my application and made it to 500-something last time. We'll see how far I got this time. :P |
@khellang |
@khellang @Mike-E-angelo |
@seesharper Yeah, it's simply a wrapper around It could bring issues if the MS.Ext.DI adapter simply calls
That's a great idea! |
@khellang @Mike-E-angelo I had to fix this in LightInject. So this quicky rippled through all packages 😃. @Mike-E-angelo . You should get away with the new version of LightInject.Microsoft.Hosting which is now at version 1.3.3. This pulls in LightInject.Microsoft.DependencyInjection 3.4.3 which in turn pulls in LightInject 6.5.2. Still possibly more work needed in LightInject as I have no tests for this right now. Late friday quick-fix. What could possibly go wrong. @khellang I might borrow |
Haha, Friday night deployments are the best 😜 Sure, borrow all you want 👍 |
Woohoo! I can confirm all my 1200+ registrations now load without error now! 🎉 THANK YOU both @khellang and @seesharper for being so quick and collaborative around this and getting out a fix. It is very much appreciated!!! |
FYI: The fix from @khellang was still needed since we create the service factory in the adapter.👍 And with excellent feedback from @Mike-E-angelo I'd say this is joint effort at its best. 👍😃. @khellang Would it make sense to publish the tests for Scrutor as a NuGet package so that other Ms.Ex.Di adapters can include tests for Scrutor? 😀 |
I guess. The library was always meant as a thin layer on top of MS.Ext.DI, but over time, as we've tried to squeeze as much as we can out of the built-in container, I guess we've reached a point where we're starting to feel the pain of the conforming container pattern 😅 |
This is a continuation of this comment:
#171 (comment)
I have created a SLN/failing test demonstrating this issue here:
https://github.com/Mike-E-angelo/Stash/tree/master/Scrutor.Decorate
Reverting to 4.1.0 removes this issue.
Please do let me know if there is any further information that I can provide and I will assist.
Thank you for any consideration/effort you can provide towards addressing this. 🙏
The text was updated successfully, but these errors were encountered: