From 20056a184878140a096b07fd49db16fa0d15f486 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Tue, 23 Mar 2021 17:28:22 -0700 Subject: [PATCH] Dispose the DiagnosticListener - Add the DiagnosticListener to the DI container as a lambda so it gets disposed. --- src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs | 5 ++--- src/Hosting/Hosting/src/WebHostBuilder.cs | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs index f694434b5746..0629eb2bc785 100644 --- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs @@ -84,9 +84,8 @@ public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options // REVIEW: This is bad since we don't own this type. Anybody could add one of these and it would mess things up // We need to flow this differently - var listener = new DiagnosticListener("Microsoft.AspNetCore"); - services.TryAddSingleton(listener); - services.TryAddSingleton(listener); + services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore")); + services.TryAddSingleton(sp => sp.GetRequiredService()); services.TryAddSingleton(); services.TryAddScoped(); diff --git a/src/Hosting/Hosting/src/WebHostBuilder.cs b/src/Hosting/Hosting/src/WebHostBuilder.cs index 4927fa8f8672..0e809be7cf1e 100644 --- a/src/Hosting/Hosting/src/WebHostBuilder.cs +++ b/src/Hosting/Hosting/src/WebHostBuilder.cs @@ -283,9 +283,8 @@ private IServiceCollection BuildCommonServices(out AggregateException? hostingSt services.AddSingleton(_ => configuration); _context.Configuration = configuration; - var listener = new DiagnosticListener("Microsoft.AspNetCore"); - services.AddSingleton(listener); - services.AddSingleton(listener); + services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore")); + services.TryAddSingleton(sp => sp.GetRequiredService()); services.AddTransient(); services.AddTransient();