Skip to content

Commit 80880d0

Browse files
authored
Dispose the DiagnosticListener (#31170)
- Add the DiagnosticListener to the DI container as a lambda so it gets disposed.
1 parent 7fb760f commit 80880d0

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options
8484

8585
// REVIEW: This is bad since we don't own this type. Anybody could add one of these and it would mess things up
8686
// We need to flow this differently
87-
var listener = new DiagnosticListener("Microsoft.AspNetCore");
88-
services.TryAddSingleton<DiagnosticListener>(listener);
89-
services.TryAddSingleton<DiagnosticSource>(listener);
87+
services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore"));
88+
services.TryAddSingleton<DiagnosticSource>(sp => sp.GetRequiredService<DiagnosticListener>());
9089

9190
services.TryAddSingleton<IHttpContextFactory, DefaultHttpContextFactory>();
9291
services.TryAddScoped<IMiddlewareFactory, MiddlewareFactory>();

src/Hosting/Hosting/src/WebHostBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,8 @@ private IServiceCollection BuildCommonServices(out AggregateException? hostingSt
283283
services.AddSingleton<IConfiguration>(_ => configuration);
284284
_context.Configuration = configuration;
285285

286-
var listener = new DiagnosticListener("Microsoft.AspNetCore");
287-
services.AddSingleton<DiagnosticListener>(listener);
288-
services.AddSingleton<DiagnosticSource>(listener);
286+
services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore"));
287+
services.TryAddSingleton<DiagnosticSource>(sp => sp.GetRequiredService<DiagnosticListener>());
289288

290289
services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
291290
services.AddTransient<IHttpContextFactory, DefaultHttpContextFactory>();

0 commit comments

Comments
 (0)