From 0bc85649bb2e5e960c656248297a585776ec96b0 Mon Sep 17 00:00:00 2001 From: Ramon Smits Date: Fri, 1 Mar 2024 11:44:12 +0100 Subject: [PATCH] Properly dispose created host instance (#3923) --- .../Hosting/Commands/ImportFailedAuditsCommand.cs | 2 +- .../Infrastructure/Hosting/Commands/RunCommand.cs | 3 ++- .../Infrastructure/MaintenanceBootstrapper.cs | 6 ++++-- .../Hosting/Commands/RunCommand.cs | 2 +- .../Hosting/Commands/ImportFailedErrorsCommand.cs | 2 +- src/ServiceControl/Hosting/Commands/RunCommand.cs | 2 +- 6 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/ImportFailedAuditsCommand.cs b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/ImportFailedAuditsCommand.cs index 1553eebc47..7ad87ed42f 100644 --- a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/ImportFailedAuditsCommand.cs +++ b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/ImportFailedAuditsCommand.cs @@ -28,7 +28,7 @@ public override async Task Execute(HostArguments args, Settings settings) tokenSource.Cancel(); return Task.CompletedTask; }, settings, endpointConfiguration, loggingSettings); - var app = hostBuilder.Build(); + using var app = hostBuilder.Build(); app.UseServiceControlAudit(); diff --git a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/RunCommand.cs b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/RunCommand.cs index c4783f255e..c3d5380255 100644 --- a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/RunCommand.cs +++ b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/RunCommand.cs @@ -21,7 +21,8 @@ public override async Task Execute(HostArguments args, Settings settings) //Do nothing. The transports in NSB 8 are designed to handle broker outages. Audit ingestion will be paused when broker is unavailable. return Task.CompletedTask; }, settings, endpointConfiguration, loggingSettings); - var app = hostBuilder.Build(); + + using var app = hostBuilder.Build(); app.UseServiceControlAudit(); await app.RunAsync(); diff --git a/src/ServiceControl.Audit/Infrastructure/MaintenanceBootstrapper.cs b/src/ServiceControl.Audit/Infrastructure/MaintenanceBootstrapper.cs index e8cc1f35d1..f40173a7c1 100644 --- a/src/ServiceControl.Audit/Infrastructure/MaintenanceBootstrapper.cs +++ b/src/ServiceControl.Audit/Infrastructure/MaintenanceBootstrapper.cs @@ -18,15 +18,17 @@ public static async Task Run(Settings.Settings settings) var hostBuilder = Host.CreateApplicationBuilder(); hostBuilder.Services.AddPersistence(persistenceSettings, persistenceConfiguration); + using var host = hostBuilder.Build(); + if (WindowsServiceHelpers.IsWindowsService()) { - await hostBuilder.Build().RunAsync(); + await host.RunAsync(); } else { await Console.Out.WriteLineAsync("Running in Maintenance Mode - Press CTRL+C to exit"); - await hostBuilder.Build().RunAsync(); + await host.RunAsync(); await Console.Out.WriteLineAsync("Disposing persister (this might take a while)..."); } diff --git a/src/ServiceControl.Monitoring/Hosting/Commands/RunCommand.cs b/src/ServiceControl.Monitoring/Hosting/Commands/RunCommand.cs index 3890b7c72b..99917a242f 100644 --- a/src/ServiceControl.Monitoring/Hosting/Commands/RunCommand.cs +++ b/src/ServiceControl.Monitoring/Hosting/Commands/RunCommand.cs @@ -13,7 +13,7 @@ public override async Task Execute(Settings settings) var hostBuilder = WebApplication.CreateBuilder(); hostBuilder.AddServiceControlMonitoring((_, __) => Task.CompletedTask, settings, endpointConfiguration); - var app = hostBuilder.Build(); + using var app = hostBuilder.Build(); app.UseServiceControlMonitoring(); await app.RunAsync(); diff --git a/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs b/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs index 0e32fd17a1..58445999f8 100644 --- a/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs +++ b/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs @@ -28,7 +28,7 @@ public override async Task Execute(HostArguments args, Settings settings) // TODO: Ideally we would never want to actually bootstrap the web api. Figure out how var hostBuilder = WebApplication.CreateBuilder(); hostBuilder.AddServiceControl(settings, endpointConfiguration, loggingSettings); - var app = hostBuilder.Build(); + using var app = hostBuilder.Build(); app.UseServiceControl(); await app.StartServiceControl(); diff --git a/src/ServiceControl/Hosting/Commands/RunCommand.cs b/src/ServiceControl/Hosting/Commands/RunCommand.cs index 31e790d8b1..31c5f01820 100644 --- a/src/ServiceControl/Hosting/Commands/RunCommand.cs +++ b/src/ServiceControl/Hosting/Commands/RunCommand.cs @@ -22,7 +22,7 @@ public override async Task Execute(HostArguments args, Settings settings) var hostBuilder = WebApplication.CreateBuilder(); hostBuilder.AddServiceControl(settings, endpointConfiguration, loggingSettings); - var app = hostBuilder.Build(); + using var app = hostBuilder.Build(); app.UseServiceControl(); await app.StartServiceControl();