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
<PackageReference Include="Aspire.Azure.Npgsql" Version="9.3.1" />
<PackageVersion Include="Aspire.Azure.Npgsql" Version="9.3.1" />
<PackageReference Include="Aspire.Azure.Npgsql" />
paket add Aspire.Azure.Npgsql --version 9.3.1
#r "nuget: Aspire.Azure.Npgsql, 9.3.1"
#:package Aspire.Azure.Npgsql@9.3.1
#addin nuget:?package=Aspire.Azure.Npgsql&version=9.3.1
#tool nuget:?package=Aspire.Azure.Npgsql&version=9.3.1
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
- PostgreSQL database and connection string for accessing the database.
- or an Azure Database for PostgreSQL instance, learn more about how to Create an Azure Database for PostgreSQL resource.
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
- https://www.npgsql.org/doc/basic-usage.html
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
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 | Versions 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. |
-
net8.0
- Aspire.Npgsql (>= 9.3.1)
- AspNetCore.HealthChecks.NpgSql (>= 9.0.0)
- Azure.Core (>= 1.46.0)
- Azure.Identity (>= 1.13.2)
- Microsoft.Extensions.Azure (>= 1.11.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.15)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Primitives (>= 8.0.0)
- Npgsql.DependencyInjection (>= 9.0.3)
- Npgsql.OpenTelemetry (>= 9.0.3)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
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
|