Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong document for adding Azure authenticated NpgsqlDbContext #2081

Open
RamType0 opened this issue Nov 13, 2024 · 4 comments
Open

Wrong document for adding Azure authenticated NpgsqlDbContext #2081

RamType0 opened this issue Nov 13, 2024 · 4 comments

Comments

@RamType0
Copy link

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.

@davidfowl davidfowl transferred this issue from dotnet/aspire Nov 13, 2024
@jjkopmels
Copy link

same issue here

@matt-goldman
Copy link

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

@matt-goldman
Copy link

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
Copy link

@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);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants