# AspireAzureNpgsqlExtensions Methods

- Package: [Aspire.Azure.Npgsql](/reference/api/csharp/aspire.azure.npgsql.md)
- Type: [AspireAzureNpgsqlExtensions](/reference/api/csharp/aspire.azure.npgsql/aspireazurenpgsqlextensions.md)
- Kind: `Methods`
- Members: `2`

Extension methods for connecting to an Azure Database for PostgreSQL with Npgsql client

## AddAzureNpgsqlDataSource(IHostApplicationBuilder, string, Action<AzureNpgsqlSettings>, Action<NpgsqlDataSourceBuilder>)

- Name: `AddAzureNpgsqlDataSource(IHostApplicationBuilder, string, Action<AzureNpgsqlSettings>, Action<NpgsqlDataSourceBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.Npgsql/AspireAzureNpgsqlExtensions.cs#L31-L44)

Registers `NpgsqlDataSource` service for connecting PostgreSQL database with Npgsql client. Configures health check, logging and telemetry for the Npgsql client.

```csharp
public static class AspireAzureNpgsqlExtensions
{
    public static void AddAzureNpgsqlDataSource(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<AzureNpgsqlSettings>? configureSettings = null,
        Action<NpgsqlDataSourceBuilder>? configureDataSourceBuilder = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  A name used to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<AzureNpgsqlSettings>`) `optional`
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.
- `configureDataSourceBuilder` (`Action<NpgsqlDataSourceBuilder>`) `optional`
  An optional delegate that can be used for customizing the `NpgsqlDataSourceBuilder`.

## Exceptions

- `ArgumentNullException` -- Thrown if mandatory `builder` is null.
- `InvalidOperationException` -- Thrown when mandatory `NpgsqlSettings.ConnectionString` is not provided or the [AzureNpgsqlSettings.Credential](/reference/api/csharp/aspire.azure.npgsql/azurenpgsqlsettings/properties.md#credential) is invalid.

## Remarks

Reads the configuration from "Aspire:Npgsql" section.

## AddKeyedAzureNpgsqlDataSource(IHostApplicationBuilder, string, Action<AzureNpgsqlSettings>, Action<NpgsqlDataSourceBuilder>)

- Name: `AddKeyedAzureNpgsqlDataSource(IHostApplicationBuilder, string, Action<AzureNpgsqlSettings>, Action<NpgsqlDataSourceBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.Npgsql/AspireAzureNpgsqlExtensions.cs#L60-L73)

Registers `NpgsqlDataSource` as a keyed service for given `name` for connecting PostgreSQL database with Npgsql client. Configures health check, logging and telemetry for the Npgsql client.

```csharp
public static class AspireAzureNpgsqlExtensions
{
    public static void AddKeyedAzureNpgsqlDataSource(
        this IHostApplicationBuilder builder,
        string name,
        Action<AzureNpgsqlSettings>? configureSettings = null,
        Action<NpgsqlDataSourceBuilder>? configureDataSourceBuilder = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<AzureNpgsqlSettings>`) `optional`
  An optional method that can be used for customizing options. It's invoked after the settings are read from the configuration.
- `configureDataSourceBuilder` (`Action<NpgsqlDataSourceBuilder>`) `optional`
  An optional delegate that can be used for customizing the `NpgsqlDataSourceBuilder`.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` or `name` is null.
- `ArgumentException` -- Thrown if mandatory `name` is empty.
- `InvalidOperationException` -- Thrown when mandatory `NpgsqlSettings.ConnectionString` is not provided or the [AzureNpgsqlSettings.Credential](/reference/api/csharp/aspire.azure.npgsql/azurenpgsqlsettings/properties.md#credential) is invalid.

## Remarks

Reads the configuration from "Aspire:Npgsql:{name}" section.
