Dispose doesnt work when page is refreshed (server-side blazor)
See original GitHub issueHello I was trying to make the fetchdata example update values without refreshing page or clicking buttons. And I found out that I can use timer which seemed like working ok. But after testing with different data it was acting strange and I found out that page doesnt dispose the timer when I go to different page and its not displayed anymore. That was solved by adding dispose but it didnt solve different issue… When I press F5 while on /fetchdata page it reloads and doesnt dispose the timer. And then it runs twice. I tried it by adding int into ForecastService and it increment everytime GetForecastAsync is called. After 10 refreshes it increment +10 every second instead of +1. I hope it makes sense.
@functions {
WeatherForecast[] forecasts;
Timer timer;
private async void Update()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
StateHasChanged();
}
protected override async Task OnInitAsync()
{
timer = new Timer(1000);
timer.Elapsed += (sender, args) => Update();
timer.Start();
Update();
}
public void Dispose()
{
timer.Dispose();
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (9 by maintainers)
Top Results From Across the Web
JSDisconnectedException Thrown on Page Refresh at ...
DisposeAsync method, I receive an error in Blazor Server whenever a disposal attempt is made (such as on page refresh, for example):
Read more >ASP.NET Core Blazor performance best practices
Tips for increasing performance in ASP.NET Core Blazor apps and avoiding common performance problems.
Read more >ASP.NET Core Razor component lifecycle
When object disposal is initiated from either the .NET or JS side, Blazor removes the entry from the map, and the object can...
Read more >Context Instance disposed on page refresh
A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use...
Read more >Blazor Server - DbContext is disposed after refreshing page ...
Coding example for the question Blazor Server - DbContext is disposed after refreshing page-blazor.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, this is one of several areas of cleanup and resource-usage resilience we know we need to implement in the server-side model. Thanks for raising this - I’ll leave it open until we can get to it (which hopefully will be soon).
I added that to https://github.com/aspnet/AspNetCore/issues/6358