diff --git a/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs b/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs index a8174580d4..2cc12cf563 100644 --- a/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs @@ -84,8 +84,6 @@ public static IHealthChecksBuilder AddNpgSql( { options.ConnectionString ??= Guard.ThrowIfNull(connectionStringFactory.Invoke(sp), throwOnEmptyString: true, paramName: nameof(connectionStringFactory)); - ResolveDataSourceIfPossible(options, sp); - return new NpgSqlHealthCheck(options); }, failureStatus, @@ -170,29 +168,9 @@ public static IHealthChecksBuilder AddNpgSql( return builder.Add(new HealthCheckRegistration( name ?? NAME, - sp => - { - ResolveDataSourceIfPossible(options, sp); - - return new NpgSqlHealthCheck(options); - }, + _ => new NpgSqlHealthCheck(options), failureStatus, tags, timeout)); } - - private static void ResolveDataSourceIfPossible(NpgSqlHealthCheckOptions options, IServiceProvider sp) - { - if (options.DataSource is null && !options.TriedToResolveFromDI) - { - NpgsqlDataSource? fromDi = sp.GetService(); - if (fromDi?.ConnectionString == options.ConnectionString) - { - // When it's possible, we reuse the DataSource registered in the DI. - // We do that to achieve best performance and avoid issues like https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/1993 - options.DataSource = fromDi; - } - options.TriedToResolveFromDI = true; // save the answer, so we don't do it more than once - } - } } diff --git a/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs b/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs index 9712e43f66..e4171c035e 100644 --- a/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs +++ b/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs @@ -65,8 +65,6 @@ public NpgSqlHealthCheckOptions(NpgsqlDataSource dataSource) /// public NpgsqlDataSource? DataSource { get; internal set; } - internal bool TriedToResolveFromDI; - /// /// The query to be executed. /// diff --git a/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs b/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs index 21d12a0510..de1731f0c9 100644 --- a/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs +++ b/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs @@ -1,6 +1,4 @@ -using System.Reflection; using HealthChecks.NpgSql; -using Npgsql; namespace HealthChecks.Npgsql.Tests.DependencyInjection; @@ -21,7 +19,6 @@ public void add_health_check_when_properly_configured() registration.Name.ShouldBe("npgsql"); check.ShouldBeOfType(); - } [Fact] @@ -91,46 +88,6 @@ public void factory_is_called_only_once() factoryCalls.ShouldBe(1); } - [Theory] - [InlineData(true)] - [InlineData(false)] - public void factory_reuses_pre_registered_datasource_when_possible(bool sameConnectionString) - { - const string connectionString = "Host=localhost"; - ServiceCollection services = new(); - - services.AddSingleton(serviceProvider => - { - var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString); - return dataSourceBuilder.Build(); - }); - - int factoryCalls = 0; - services.AddHealthChecks() - .AddNpgSql(_ => - { - Interlocked.Increment(ref factoryCalls); - return sameConnectionString ? connectionString : $"{connectionString}2"; - }, name: "my-npg-1"); - - using var serviceProvider = services.BuildServiceProvider(); - - var options = serviceProvider.GetRequiredService>(); - - var registration = options.Value.Registrations.Single(); - - for (int i = 0; i < 10; i++) - { - var healthCheck = (NpgSqlHealthCheck)registration.Factory(serviceProvider); - var fieldInfo = typeof(NpgSqlHealthCheck).GetField("_options", BindingFlags.Instance | BindingFlags.NonPublic); - var npgSqlHealthCheckOptions = (NpgSqlHealthCheckOptions)fieldInfo!.GetValue(healthCheck)!; - - Assert.Equal(sameConnectionString, ReferenceEquals(serviceProvider.GetRequiredService(), npgSqlHealthCheckOptions.DataSource)); - } - - factoryCalls.ShouldBe(1); - } - [Fact] public void recommended_scenario_compiles_and_works_as_expected() {