-
I have a rather baffling problem with a solution that I don't understand:
Why oh why is a Singleton disposed at the end of a ServiceScope? Shouldn't a Singleton only be disposed when the root serviceprovider is disposed? Even weirder, now instead of giving this class an Can someone explain this to me? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found part of the issue. Somewhere else one of the interfaces implemented by this class was registered using: services.AddTransient<SomeInterface>(sp => sp.GetRequiredService<MyClass>()); I guess the I fixed it by changing the |
Beta Was this translation helpful? Give feedback.
I found part of the issue. Somewhere else one of the interfaces implemented by this class was registered using:
I guess the
Dispose
method was called when the scope ended, becauseSomeInterface
was transient and the underlying class wasIDisposable
. That's a bit weird, but somewhat understandable. It still doesn't explain why the problem didn't occur when I made the class extendHttpClient
. I thought maybe HttpClient implements IDisposable explicitly, and that prevents the behaviour. But I checked the code, and that's not the case.I fixed it by changing the
AddTransient<SomeInterface>
toAddSingleton<SomeInterf…