.NET Aspire SQL Server component

In this article, you learn how to use the .NET Aspire SQL Server component. The Aspire.Microsoft.Data.SqlClient library:

  • Registers a scoped Microsoft.Data.SqlClient.SqlConnection factory in the DI container for connecting Azure SQL, MS SQL database.
  • Automatically configures the following:
    • Connection pooling to efficiently managed HTTP requests and database connections
    • Automatic retries to increase app resiliency
    • Health checks, logging and telemetry to improve app monitoring and diagnostics

Prerequisites

Get started

To get started with the .NET Aspire SQL Server component, install the Aspire.Microsoft.Data.SqlClient NuGet package.

dotnet add package Aspire.Microsoft.Data.SqlClient --prerelease

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your component-consuming project, call the AddSqlServerClient extension to register a SqlConnection for use via the dependency injection container.

builder.AddSqlServerClient("sql");

To retrieve your SqlConnection object an example service:

public class ExampleService(SqlConnection client)
{
    // Use client...
}

After adding a SqlConnection, you can get the scoped SqlConnection instance using DI.

App host usage

In your app host project, register a SqlServer container and consume the connection using the following methods:

var sql = builder.AddSqlServer("sql")
                 .AddDatabase("sqldata");

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

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

builder.AddSqlServerClient("sqldata");

Configuration

The .NET Aspire SQL Server component provides multiple configuration approaches and options to meet the requirements and conventions of your project.

Use configuration providers

The .NET Aspire SQL Server supports Microsoft.Extensions.Configuration. It loads the MicrosoftDataSqlClientSettings from configuration files such as appsettings.json by using the Aspire:SqlServer:SqlClient key. If you have set up your configurations in the Aspire:SqlServer:SqlClient section, you can just call the method without passing any parameter.

The following example shows an appsettings.json file that configures some of the available options:

{
  "Aspire": {
    "SqlServer": {
      "SqlClient": {
        "ConnectionString": "YOUR_CONNECTIONSTRING",
        "HealthChecks": true,
        "Metrics": false
      }
    }
  }
}

Use inline configurations

You can also pass the Action<MicrosoftDataSqlClientSettings> delegate to set up some or all the options inline, for example to turn off the Metrics:

builder.AddSqlServerSqlClientConfig(
    static settings => settings.Metrics = false);

Configuring connections to multiple databases

If you want to add more than one SqlConnection you could use named instances. The json configuration would look like:

{
  "Aspire": {
    "SqlServer": {
      "SqlClient": {
        "INSTANCE_NAME": {
          "ServiceUri": "YOUR_URI",
          "HealthChecks": false
        }
      }
    }
  }
}

To load the named configuration section from the json config call the AddSqlServerSqlClientConfig method by passing the INSTANCE_NAME.

builder.AddSqlServerSqlClientConfig("INSTANCE_NAME");

Configuration options

Here are the configurable options with corresponding default values:

Name Description
ConnectionString The connection string of the SQL Server database to connect to.
HealthChecks A boolean value that indicates whether the database health check is enabled or not.
Tracing A boolean value that indicates whether the OpenTelemetry tracing is enabled or not.
Metrics A boolean value that indicates whether the OpenTelemetry metrics are enabled or not.

Health checks

By default, .NET Aspire components enable health checks for all services. For more information, see .NET Aspire components overview.

By default, the .NET Aspire Sql Server component handles the following:

  • Adds the SqlServerHealthCheck, which verifies that a connection can be made commands can be run against the SQL Database.
  • Integrates with the /health HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic

Observability and telemetry

.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire SQL Server component currently doesn't enable logging by default due to limitations of the SqlClient.

Tracing

The .NET Aspire SQL Server component will emit the following Tracing activities using OpenTelemetry:

  • "OpenTelemetry.Instrumentation.SqlClient"

Metrics

The .NET Aspire SQL Server component will emit the following metrics using OpenTelemetry:

  • Microsoft.Data.SqlClient.EventSource
    • active-hard-connections
    • hard-connects
    • hard-disconnects
    • active-soft-connects
    • soft-connects
    • soft-disconnects
    • number-of-non-pooled-connections
    • number-of-pooled-connections
    • number-of-active-connection-pool-groups
    • number-of-inactive-connection-pool-groups
    • number-of-active-connection-pools
    • number-of-inactive-connection-pools
    • number-of-active-connections
    • number-of-free-connections
    • number-of-stasis-connections
    • number-of-reclaimed-connections

See also