Singleton depenendency injected service depending on scoped injected service #63967
Replies: 3 comments 1 reply
-
|
For This is documented in Scope validation, Scope validation, and Scope validation (3x the same title, but different targets). Die you try this? What is missing here? |
Beta Was this translation helpful? Give feedback.
-
|
So that's interesting. I must now report that we observe that it is not working. However our startup code dates from NET Core 1.1's templates. Service startup code: |
Beta Was this translation helpful? Give feedback.
-
|
The builder looks like this: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We had this issue this past week where we discovered that a particular singleton service would die after several hours. On tracing it to the bottom we found the impossibility; the singleton service was receiving a scoped service as a dependency. The dependency injector just lets you do this.
The correct fix was as expected, pass the scoped dependency in from the controller (on each of the handful of method calls that cared) rather than the constructor of the service.
I'm left wondering if the dependency injector should have caught this mistake or not.
Basically it looked like this (boneheaded mistake):
This happened because the new guy missed it; the two services.Add lines were in the same function but not the same screen.
I'm just wondering if DI itself or an analyzer should have caught this; or on the flip side there's a real use for this construct.
Beta Was this translation helpful? Give feedback.
All reactions