Dispose doesnt work when page is refreshed (server-side blazor)

See original GitHub issue

Hello 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:closed
  • Created 5 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
SteveSandersonMScommented, Nov 9, 2018

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).

0reactions
javiercncommented, Apr 10, 2019
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found