Prevent singletons with container-scoped dependencies #266
Prevent singletons with container-scoped dependencies #266julian99m wants to merge 2 commits intomicrosoft:masterfrom
Conversation
…leton-information within the resolution context and checking it during resolution.
|
@microsoft-github-policy-service agree company="freenet DLS GmbH" |
|
Is the bug/undefined behavior we're trying to prevent, the scenario where a child container instantiates a parent container's singleton, which now has a dependency scoped to the child container? |
basically, yes. I'd say the issue is not limited to parent/child-containers, but generally to singletons using container-scoped dependencies. But the scenario you described is probably the most common one where this happens. |
|
Wait, now you got me thinking... parent container instances using child container instances is not a subset of singletons using container-scoped dependencies. Instead, they are separate scenarios that can overlap (because singletons are registered in the global container). |
|
Any opinions on this topic? |
|
bump |
2 similar comments
|
bump |
|
bump |
|
@Xapphire13 any feedback on this? |
... by passing singleton-information within the resolution context and checking it during resolution.
Conceptually, singletons should not use container-scoped dependencies.
I can not think of any use case where the previous behavior (initializing a singleton that uses one specific instance of a container-scoped dependency) would be useful. In fact, it could lead to hard-to-detect bugs by using unexpected instances.
As this could be a breaking change, it might be necessary to have the change only be activated by configuration. Please give me feedback in that regard :)