Closed CamusGao closed 3 years ago
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
HostedServices are singletons, you can't change the lifetime because they are only ever resolved once at startup and aren't resolved in a scope.
HostedServices are singletons, you can't change the lifetime because they are only ever resolved once at startup and aren't resolved in a scope.
sorry, maybe it's my mistake. I am using dotnet core 3.1. But this List kept a huge number of references of disposable transient services and comsumed memory.
Yes, that's by design and unfortunately, it's a bit of a performance trap. Don't resolve transient disposable objects from your IHostedService constructor. If you're looking to do something with a scoped lifetime then you need to explicitly created that scope and resolve services from there.
ok, but why do this design.
The DI container's is responsible for disposing these objects when the container or scope is disposed. This is true for dependencies of any service that implement IDisposable
.
Tagging subscribers to this area: @eerhardt, @maryamariyan See info in area-owners.md if you want to be subscribed.
thank you.
ServiceCollectionHostedServiceExtensions.AddHostedService add IHostedService with scoped life time and if my IHostedService using IServiceProvider to get services with transient life time do some work, those transient service cannot be dispose until this scope being disposed while IHostedService will be disposed at the end of this application life time usually.