Disposing disposable transient services in Blazor #20755
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Answered
Resolved because the question asked by the original author has been answered.
question
Status: Resolved
Apologies if this is the wrong place to post this.
Describe the bug
Disposable transient / scoped services are not disposed when there are no more references to the object in Blazor. A simple example of this can be seen from having 2 disposable pages and 2 disposable transient services.
The pages are created and destroyed at the correct time and the services are created when the page is created. However, the services are not disposed until the page is refreshed or I leave the website. Navigating to a different page on the website does not dispose them (and even if it did, that would not handle the disposing of services in sub-components on a page).
I thought about disposing the injected service when the component is disposed, but this wouldn't work for scoped / singleton dependencies and
[Inject]
had no way of saying what scope the injected dependency had (and it shouldn't as why should the component care?)The issue is, this leads to a lot of services that require disposing still running in the background. Is there a way to get around this issue?
Annotated log of when things are created and disposed
To Reproduce
It can be reproduced with two simple disposable razor pages and two services registered as transient, or it can be cloned from here.
Base.cs
PageOne.razor
PageTwo.razor
Services.cs
Services Registered in Startup.cs
Further technical details
dotnet --info
Using Visual Studio 2019 (16.5.3)
The text was updated successfully, but these errors were encountered: