Incorrect dispose order when a dependency class is registered as both Singleton and KeyedSingleton #121371
Unanswered
alexey-leonovich
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I reference Microsoft.Extensions.Hosting 8.0 in my .NET 8.0 console application.
In my host I have two classes (SecondaryClass depends on PrimaryClass). PrimaryClass is registered as both Singleton and KeyedSingleton.
I also have two hosted services (one has those classes interfaces injected directly into constructor, another one obtains PrimaryClass with
IServiceProvider.GetKeyedService<IPrimary>("key")call).And it looks like those classes dispose order depend on registration code - this initialization does not cause problems:
This initialization causes problems (PrimaryClass is disposed before SecondaryClass ):
Sample code
I've uploaded an online example to run as-is (
problem = trueshows problematic scenario, change it tofalseto see correct scenario).Is that a bug in Generic Host's DI or am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions