From 796f47f4b1c9d67812f52c5963c0eb9bc2eefdb5 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 21 Sep 2023 12:25:48 +0200 Subject: [PATCH] one more fix --- .../NpgSqlHealthCheckBuilderExtensions.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs b/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs index 0e8cfe15c4..fb0dd34653 100644 --- a/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs @@ -106,7 +106,7 @@ public static IHealthChecksBuilder AddNpgSql( name ?? NAME, sp => { - // The Data Source needs to be fromFactory only once, + // The Data Source needs to be created only once, // as each instance has it's own connection pool. // See https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/1993 for more details. @@ -123,7 +123,12 @@ public static IHealthChecksBuilder AddNpgSql( { // If they are using the same ConnectionString, we can reuse the instance from DI. // So there is only ONE NpgsqlDataSource per the whole app and ONE connection pool. - fromFactory.Dispose(); + + if (!ReferenceEquals(fromDI, fromFactory)) + { + // Dispose it, as long as it's not the same instance. + fromFactory.Dispose(); + } Interlocked.Exchange(ref dataSource, fromDI); options.DataSource = fromDI; }