Skip to content

Binding NpgsqlConnectionStringBuilder to configuration does not work on .net 7.0 #78908

Closed
@frabe1579

Description

@frabe1579

Description

I'm used to bind an instance of NpgsqlConnectionStringBuilder (from Npgsql) to the configuration during startup, but this does not work anymore after upgrading to .net 7.0: The instance remains empty and values are not loaded from configuration

Reproduction Steps

  • Create a Console Project like this:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
    <PackageReference Include="Npgsql" Version="7.0.0" />
  </ItemGroup>

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>
  • Add this to Program.cs:
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using Npgsql;

var configuration = new ConfigurationBuilder()
  .AddJsonFile("appsettings.json")
  .Build();

Debug.Assert(configuration["db:host"] == "localhost");

var csb = new NpgsqlConnectionStringBuilder();
configuration.GetSection("db").Bind(csb);
var cs = csb.ToString();

Debug.Assert(csb.Host != null);
Console.WriteLine("Host is: " + csb.Host);

Add appsettings.json file with this content:

{
  "db": {
    "host": "localhost"
  }
}

If you dotnet run, the program crashes due to the last assertion: the values are not loaded from configuration.
If the project is downgraded to .net6.0 (and also the deps), the program runs ok, the values are correctly loaded.
I think it's not a direct problem of Npgsql, but something related to a change in .net7 that is not compatible with internal mappings used by NpgsqlConnectionStringBuilder.
A direct binding to a POCO runs like a charm, but with NpgsqlConnectionStringBuilder no.

Expected behavior

The configuration data is loaded and mapped to the instance passed to Bind.

Actual behavior

The configuration data is not loaded and the instance passed to Bind reamins empty.

Regression?

Yes, this worked on .net 6.0, at least up to 6.0.10.

Known Workarounds

No direct workaraoud, it's necessary to use a POCO to bind from configuration.

Configuration

.NET 7.0
Window 11 Pro x64

Other information

The problem might be in changes to configuration binder, considering that NpgsqlConnectionStringBuilder exposes properties but also a IDictionary<string, object?>.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions