Aspire.Azure.Npgsql 9.3.1

Prefix Reserved
dotnet add package Aspire.Azure.Npgsql --version 9.3.1
                    
NuGet\Install-Package Aspire.Azure.Npgsql -Version 9.3.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aspire.Azure.Npgsql" Version="9.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aspire.Azure.Npgsql" Version="9.3.1" />
                    
Directory.Packages.props
<PackageReference Include="Aspire.Azure.Npgsql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Aspire.Azure.Npgsql --version 9.3.1
                    
#r "nuget: Aspire.Azure.Npgsql, 9.3.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Aspire.Azure.Npgsql@9.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Aspire.Azure.Npgsql&version=9.3.1
                    
Install as a Cake Addin
#tool nuget:?package=Aspire.Azure.Npgsql&version=9.3.1
                    
Install as a Cake Tool

Aspire.Azure.Npgsql library

Registers NpgsqlDataSource in the DI container for connecting to PostgreSQL and Azure Database for PostgreSQL. Enables corresponding health check, metrics, logging and telemetry.

Getting started

Prerequisites

Differences with Aspire.Npgsql

The Aspire.Azure.Npgsql library is a wrapper around the Aspire.Npgsql library that provides additional features for connecting to Azure Database for PostgreSQL. If you don't need these features, you can use the Aspire.Npgsql library instead. At runtime the client integration will detect whether the connection string has a Username and Password, and if not, it will use Entra Id to authenticate with Azure Database for PostgreSQL.

Install the package

Install the .NET Aspire Azure Npgsql library with NuGet:

dotnet add package Aspire.Azure.Npgsql

Usage example

In the AppHost.cs file of your project, call the AddAzureNpgsqlDataSource extension method to register a NpgsqlDataSource for use via the dependency injection container. The method takes a connection name parameter.

builder.AddAzureNpgsqlDataSource("postgresdb");

You can then retrieve the NpgsqlDataSource instance using dependency injection. For example, to retrieve the data source from a Web API controller:

private readonly NpgsqlDataSource _dataSource;

public ProductsController(NpgsqlDataSource dataSource)
{
    _dataSource = dataSource;
}

Configuration

The .NET Aspire Azure Npgsql component provides multiple options to configure the database connection based on the requirements and conventions of your project.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddAzureNpgsqlDataSource():

builder.AddAzureNpgsqlDataSource("myConnection");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "myConnection": "Host=myserver;Database=test"
  }
}

See the ConnectionString documentation for more information on how to format this connection string.

Note that the username and password will be automatically inferred from the credential provided in the settings.

Use configuration providers

The .NET Aspire Azure Npgsql component supports Microsoft.Extensions.Configuration. It loads the AzureNpgsqlSettings from configuration by using the Aspire:Npgsql key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Npgsql": {
      "DisableHealthChecks": true,
      "DisableTracing": true
    }
  }
}

Use inline delegates

Also you can pass the Action<AzureNpgsqlSettings> configureSettings delegate to set up some or all the options inline, for example to disable health checks from code:

    builder.AddAzureNpgsqlDataSource("postgresdb", settings => settings.DisableHealthChecks = true);

Use the AzureNpgsqlSettings.Credential property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.

If the connection string contains a username and a password then the credential will be ignored.

AppHost extensions

In your AppHost project, install the Aspire.Hosting.Azure.PostgreSQL library with NuGet:

dotnet add package Aspire.Hosting.Azure.PostgreSQL

Then, in the AppHost.cs file of AppHost, register a Azure Database for PostgreSQL instance and consume the connection using the following methods:

var postgresdb = builder.AddAzurePostgresFlexibleServer("pg").AddDatabase("postgresdb");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(postgresdb);

The WithReference method configures a connection in the MyService project named postgresdb. In the Program.cs file of MyService, the database connection can be consumed using:

builder.AddAzureNpgsqlDataSource("postgresdb");

This will also require your Azure environment to be configure by following these instructions.

Troubleshooting

In the rare case that the Username property is not provided and the integration can't detect it using the application's Managed Identity, Npgsql will throw an exception like the following:

Npgsql.PostgresException (0x80004005): 28P01: password authentication failed for user ...

In that case you can configure the Username property in the connection string by using the configureDataSourceBuilder callback like so:

builder.AddAzureNpgsqlDataSource("db", configureDataSourceBuilder:
  dataSourceBuilder => dataSourceBuilder.ConnectionStringBuilder.Username = "<PRINCIPALNAME>");

Additional documentation

Feedback & contributing

https://github.com/dotnet/aspire

*Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Aspire.Azure.Npgsql:

Repository Stars
foxminchan/BookWorm
The practical implementation of .NET Aspire using Microservices