diff --git a/src/paradigm-ehb.CommandCenter.Core/Models/AgentClient.cs b/src/paradigm-ehb.CommandCenter.Core/Models/AgentClient.cs index 8501f61..4c5fe1d 100644 --- a/src/paradigm-ehb.CommandCenter.Core/Models/AgentClient.cs +++ b/src/paradigm-ehb.CommandCenter.Core/Models/AgentClient.cs @@ -8,7 +8,7 @@ namespace paradigm_ehb.CommandCenter.Core.Models { - public sealed class AgentClient : IDisposable + public sealed class AgentClient : IDisposable, IAsyncDisposable { private bool disposedValue; private AgentHealthWatcher? _healthWatcher; @@ -63,6 +63,7 @@ private void Dispose(bool disposing) { StopHealthWatch(); _healthWatcher?.Dispose(); + Channel.ShutdownAsync().Wait(); Channel?.Dispose(); } catch @@ -77,7 +78,33 @@ private void Dispose(bool disposing) public void Dispose() { Dispose(disposing: true); - GC.SuppressFinalize(this); + } + + private async Task DisposeAsync(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + try + { + StopHealthWatch(); + _healthWatcher?.Dispose(); + await Channel.ShutdownAsync().ConfigureAwait(false); + Channel?.Dispose(); + } + catch + { + // ignore + } + } + disposedValue = true; + } + } + + public async ValueTask DisposeAsync() + { + await DisposeAsync(true); } } } diff --git a/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/ServerMainPage.xaml.cs b/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/ServerMainPage.xaml.cs index 60f4f38..01e789d 100644 --- a/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/ServerMainPage.xaml.cs +++ b/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/ServerMainPage.xaml.cs @@ -38,6 +38,15 @@ protected override void OnNavigatedTo(NavigationEventArgs e) _ = InitializeForNavigationAsync(e); } + protected override async void OnNavigatedFrom(NavigationEventArgs e) + { + base.OnNavigatedFrom(e); + + AgentClient? agentClient = await _agentClientRegistry.GetAsync(serverObj.Id); + if (agentClient is not null) await agentClient.DisposeAsync(); + await _agentClientRegistry.DeregisterAsync(serverObj.Id); + } + private async Task InitializeForNavigationAsync(NavigationEventArgs e) { try diff --git a/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/Views/ServicesPage.xaml.cs b/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/Views/ServicesPage.xaml.cs index 0b0d243..d78cd8d 100644 --- a/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/Views/ServicesPage.xaml.cs +++ b/src/paradigm-ehb.CommandCenter.WinUI/srvMgnt/Views/ServicesPage.xaml.cs @@ -360,7 +360,8 @@ private async Task LoadAllServices(CancellationToken cancellationToken = default GetUnitsReply? response = await client.Service.GetAllUnitsAsync(request: new GetUnitsRequest(), cancellationToken: cancellationToken); if (response is null) { - throw new InvalidOperationException("Received null response from ActionAsync."); + await ShowErrorInfoBarAsync("Failed to retrieve services: received null response from agent."); + return; } foreach (LoadedUnit? unit in response.Units)