docs-aspire icon indicating copy to clipboard operation
docs-aspire copied to clipboard

Wrong document for adding Azure authenticated NpgsqlDbContext

Open RamType0 opened this issue 2 years ago • 4 comments

As of Aspire 9.0, Azure Database for PostgreSQL resources were updated to use Microsoft Entra ID by default.

Then, I start integration according to this article.

It says

builder.AddNpgsqlDbContext<YourDbContext>(
    "postgresdb", 
    configureDataSourceBuilder: (dataSourceBuilder) =>
{
    if (!string.IsNullOrEmpty(dataSourceBuilder.ConnectionStringBuilder.Password))
    {
        return;
    }

    dataSourceBuilder.UsePeriodicPasswordProvider(async (_, ct) =>
    {
        var credentials = new DefaultAzureCredential();
        var token = await credentials.GetTokenAsync(
            new TokenRequestContext([
                "https://ossrdbms-aad.database.windows.net/.default"
            ]), ct);

        return token.Token;
    },
    TimeSpan.FromHours(24),
    TimeSpan.FromSeconds(10));
});

but no such overload for AddNpgsqlDbContext exists.

RamType0 avatar Nov 13 '24 08:11 RamType0

same issue here

jjkopmels avatar Nov 15 '24 16:11 jjkopmels

Hey @davidfowl 👋 I see you've transferred this to docs from the Aspire repo, but I think this is actually an Aspire issue.

The overload referred to was added to src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs (see: https://github.com/dotnet/aspire/pull/1481), however it is included in the Aspire.NpqSql nuget package v 8.2.2, but not the Aspire.NpqSql.EntityFrameworkCore.PostgreSQL package v 8.2.2, and in neither for v 9.0.0.

I'm not sure why, the overload is still there on the main branch: https://github.com/dotnet/aspire/blob/ad430a1415c44c7de17651f09a10cc23057c3386/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs#L33

Although there seems to only be an extension for AddNpgsqlDataSource, and not for AddNpgsqlDbContext (so no EF Core support for periodic password provider).

Apologies if I've misunderstood, this is just from me poking around, I don't 100% understand what I'm looking at here. But it looks like a problem on the Apsire side, not the docs side.

cc: @aaronpowell

matt-goldman avatar Nov 22 '24 23:11 matt-goldman

Hey @davidfowl 👋 I see you've transferred this to docs from the Aspire repo, but I think this is actually an Aspire issue.

The overload referred to was added to src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs (see: dotnet/aspire#1481), however it is included in the Aspire.NpqSql nuget package v 8.2.2, but not the Aspire.NpqSql.EntityFrameworkCore.PostgreSQL package v 8.2.2, and in neither for v 9.0.0.

I'm not sure why, the overload is still there on the main branch: https://github.com/dotnet/aspire/blob/ad430a1415c44c7de17651f09a10cc23057c3386/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs#L33

Although there seems to only be an extension for AddNpgsqlDataSource, and not for AddNpgsqlDbContext (so no EF Core support for periodic password provider).

Apologies if I've misunderstood, this is just from me poking around, I don't 100% understand what I'm looking at here. But it looks like a problem on the Apsire side, not the docs side.

cc: @aaronpowell

Apologies, I was completely wrong. It is in fact still there in 9.0.0, and the docs do in fact only specify AddNpgsqlDataSource for the periodic password provider.

So definitely not a docs issue, and I guess adding it for EF Core is a feature request?

matt-goldman avatar Nov 23 '24 00:11 matt-goldman

@RamType0 @jjkopmels this is working for me:

builder.AddNpgsqlDataSource(
    ServiceConstants.APP_DATABASE, // I've got all my service names set as constants in a shared project
    configureDataSourceBuilder: (dataSourceBuilder) =>
    {
        if (!string.IsNullOrEmpty(dataSourceBuilder.ConnectionStringBuilder.Password))
        {
            return;
        }

        dataSourceBuilder.UsePeriodicPasswordProvider(async (_, ct) =>
        {
            var credentials = new DefaultAzureCredential();
            var token = await credentials.GetTokenAsync(
                new TokenRequestContext([
                    "https://ossrdbms-aad.database.windows.net/.default"
                ]), ct);

            return token.Token;
        },
        TimeSpan.FromHours(24),
        TimeSpan.FromSeconds(10));
    });


builder.Services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
{
    var dataSource = serviceProvider.GetRequiredService<NpgsqlDataSource>();
    options.UseNpgsql(dataSource);
});

matt-goldman avatar Nov 23 '24 21:11 matt-goldman

Closing as the docs have migrated to https://github.com/microsoft/aspire.dev. The incorrect AddNpgsqlDbContext API sample may still exist in the new docs — please file on microsoft/aspire.dev if still relevant.

IEvangelist avatar Mar 09 '26 14:03 IEvangelist