Skip to content

IDisposable singleton is selectively disposed at the end of ServiceScope #93102

Closed Answered by Ghostbird
Ghostbird asked this question in Q&A
Discussion options

You must be logged in to vote

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 Dispose method was called when the scope ended, because SomeInterface was transient and the underlying class was IDisposable. That's a bit weird, but somewhat understandable. It still doesn't explain why the problem didn't occur when I made the class extend HttpClient. 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> to AddSingleton<SomeInterf…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Ghostbird
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant