Перейти к содержимому

Azure SQL Database client integration

Это содержимое пока не доступно на вашем языке.

Azure SQL Database logo

The Aspire Azure SQL Database client integration is used to connect to an Azure SQL database using Microsoft.Data.SqlClient. To get started with the Aspire Azure SQL Database client integration, install the 📦 Aspire.Microsoft.Data.SqlClient NuGet package.

.NET CLI — Add Aspire.Microsoft.Data.SqlClient package
dotnet add package Aspire.Microsoft.Data.SqlClient

For an introduction to working with the Azure SQL Database client integration, see Get started with the Azure SQL Database integration.

In the Program.cs file of your client-consuming project, call the AddSqlServerClient extension method to register a SqlConnection for use via the dependency injection container. The method takes a connection name parameter:

builder.AddSqlServerClient(connectionName: "database");

After adding the SqlConnection, you can retrieve the connection instance using dependency injection:

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

For more information, see:

Properties of the Azure SQL Database resources

Section titled “Properties of the Azure SQL Database resources”

When you use the WithReference method to pass an Azure SQL Database resource from the AppHost project to a consuming client project, several properties are available to use in the consuming project.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Host property of a resource called database becomes DATABASE_HOST.

The Azure SQL Server resource exposes the following connection properties:

Property NameDescription
HostThe server fully qualified domain name
PortThe SQL Server port (default: 1433)
UriThe connection URI (e.g., mssql://{host}:1433)
JdbcConnectionStringJDBC-format connection string (e.g., jdbc:sqlserver://{host}:1433;encrypt=true;trustServerCertificate=false)

The Azure SQL Database resource inherits all properties from its parent server and adds:

Property NameDescription
DatabaseNameThe name of the database

For example, if you reference a database resource named database in your AppHost project, the following environment variables will be available in the consuming project:

  • DATABASE_HOST
  • DATABASE_PORT
  • DATABASE_URI
  • DATABASE_JDBCCONNECTIONSTRING
  • DATABASE_DATABASENAME

There might be situations where you want to register multiple SqlConnection instances with different connection names. To register keyed SQL clients, call the AddKeyedSqlServerClient method:

builder.AddKeyedSqlServerClient(name: "primary-db");
builder.AddKeyedSqlServerClient(name: "secondary-db");

Then you can retrieve the connection instances using dependency injection:

public class ExampleService(
[KeyedService("primary-db")] SqlConnection primaryConnection,
[KeyedService("secondary-db")] SqlConnection secondaryConnection)
{
// Use connections...
}

For more information, see Keyed services in .NET.

The Aspire Azure SQL Database library provides multiple options to configure the SQL connection based on the requirements and conventions of your project. A ConnectionString is required.

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling AddSqlServerClient:

builder.AddSqlServerClient(connectionName: "database");

The connection information is retrieved from the ConnectionStrings configuration section:

{
"ConnectionStrings": {
"database": "Server=myserver;Database=mydb;User Id=myuser;Password=mypassword;Encrypt=True"
}
}

The library supports Microsoft.Extensions.Configuration. It loads settings from configuration using the Aspire:Microsoft:Data:SqlClient key:

{
"Aspire": {
"Microsoft": {
"Data": {
"SqlClient": {
"DisableHealthChecks": true,
"DisableTracing": false,
"DisableMetrics": false
}
}
}
}
}

You can configure settings inline:

builder.AddSqlServerClient(
"database",
settings => settings.DisableHealthChecks = true);

Aspire integrations automatically set up Logging, Tracing, and Metrics configurations.

The Aspire Azure SQL Database integration uses the following log categories:

  • Microsoft.Data.SqlClient

The Aspire Azure SQL Database integration will emit the following tracing activities using OpenTelemetry:

  • Microsoft.Data.SqlClient

The Aspire Azure SQL Database integration will emit the following metrics using OpenTelemetry:

  • Microsoft.Data.SqlClient
    • db.client.connections.create_time
    • db.client.connections.use_time
    • db.client.connections.wait_time
    • db.client.connections.idle.max
    • db.client.connections.idle.min
    • db.client.connections.max
    • db.client.connections.pending_requests
    • db.client.connections.timeouts
    • db.client.connections.usage