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();