Dispose is never called for any server-side blazor components

We have a .Net Core 6 app, which we are slowly transitioning UI functionality from JS to Blazor, using Blazor Server components. For the most part everything works fine, however we introduced a couple components that have timers for refreshing status of items that can change outside of the user actions, things like network status. Everything works fine, except that when moving away from the page that the component is on, the component’s Dispose method is never called, which leaves the timer running.

Here is how the component is called from the cshtml page:

<component type="typeof(APStatus)" render-mode="Server" />

Here is the timer definition in the OnInitializedAsync.

_timer = new System.Timers.Timer(10000);
_timer.Elapsed += CountDownTimer;
_timer.Start();
_timer.Enabled = true;

Here is the CountDownTimer callback:

public async void CountDownTimer(Object source, System.Timers.ElapsedEventArgs e)
{
    if (_internalCounter > 0)
    {
        _internalCounter -= 1;
    }
    else
    {
        _timer.Enabled = false;
        _timer.Stop();
    }
    await InvokeAsync(async () =>
    {
        if (NetInfo != null)
        {
            await GetApStatus();
            
        }
    });
}

Here is Dispose Method:

public void Dispose()
{
    System.Diagnostics.Debug.WriteLine("Disposing Happened");
    _timer.Enabled = false;
    _timer.Stop();
    _timer.Dispose();
    _timer = null;   
}

I added a CircuitHandler, same problem, I get OnCircuitOpenAsync events, but the Closed Circuit never fires.

Here is some of the CircuitHandler code:

public override Task OnCircuitClosedAsync(Circuit circuit, CancellationToken cancellationToken)
{
    Console.WriteLine("OnCircuitClosedAsync");
    Circuits.TryRemove(circuit.Id, out var circuitRemoved);
    OnCircuitsChanged();
    return base.OnCircuitClosedAsync(circuit, cancellationToken);
}

I have tried virtually everything I can think of, gone through the Program.cs, various ways of calling the component, everything seems to be working fine from the chrome dev tools standpoint, no strange error messages in the logs.

I turned on blazor logging, and that hasn’t shown anything.

I have searched virtually every forum I can find for someone with a similar problem, but can’t find anything on why this would be occurring.

I have other blazor components on site, everything is working fine. I’ve added Dispose to all of those, and none of them ever get fired.

I’ve updated to the latest .net core 6 packages, no luck.

I started a fresh project, and implemented the Microsoft code for a timer in a blazor component, it works fine, the Dispose is called as expected when navigating away from the page.

When I copied that component into our project, everything works fine, but when I navigate away from the page with the component on, nothing, Dispose is never called.

Here is the counter Example:

@page "/counter"
@using System.Timers
@using System.Diagnostics
@implements IDisposable
<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<p>Current count: @timerCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
    private int timerCount = 0;
    private Timer timer = new(1000);
    
    protected override void OnInitialized()
    {
        timer.Elapsed += (sender, eventArgs) => OnTimerCallback();
        timer.Start();
    }
    
    private void OnTimerCallback()
    {
        _ = InvokeAsync(() =>
        {
            timerCount++;
            StateHasChanged();
        });
    }
    
    public void Dispose()
    {
        Debug.WriteLine("Disposing");
        timer.Dispose();
    }

}

New contributor

Jim Miller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật