I am using Blazor server and implement IDisposable
in my page components and some of their sub components, but I don't hit Dispose()
method when I close the browser tab.
Any idea how to dispose components and their dependencies when tab is closed? My dispose method looks like this:
CancellationTokenSource cts = new();
public override void Dispose()
{
this.cts?.Cancel(false);
this.cts?.Dispose();
base.Dispose();
}
According to the microsoft
If a component implements IDisposable, IAsyncDisposable, or both, the framework calls for unmanaged resource disposal when the component is removed from the UI. Disposal can occur at any time, including during component initialization.
Therefore, your dispose method will be called automatically after closing the browser.
Another link of Syncfusion:
If a component implements IDisposable, the Dispose method will be called when the component is removed from the UI.