Azure SQL Database client integration
Esta página aún no está disponible en tu idioma.
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.
dotnet add package Aspire.Microsoft.Data.SqlClient#:package Aspire.Microsoft.Data.SqlClient@*<PackageReference Include="Aspire.Microsoft.Data.SqlClient" Version="*" />For an introduction to working with the Azure SQL Database client integration, see Get started with the Azure SQL Database integration.
Add SQL Server client
Section titled “Add SQL Server client”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:
- Microsoft.Data.SqlClient documentation for examples on using the
SqlConnection. - Dependency injection in .NET for details on dependency injection.
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.
Azure SQL Server
Section titled “Azure SQL Server”The Azure SQL Server resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Host | The server fully qualified domain name |
Port | The SQL Server port (default: 1433) |
Uri | The connection URI (e.g., mssql://{host}:1433) |
JdbcConnectionString | JDBC-format connection string (e.g., jdbc:sqlserver://{host}:1433;encrypt=true;trustServerCertificate=false) |
Azure SQL Database
Section titled “Azure SQL Database”The Azure SQL Database resource inherits all properties from its parent server and adds:
| Property Name | Description |
|---|---|
DatabaseName | The 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_HOSTDATABASE_PORTDATABASE_URIDATABASE_JDBCCONNECTIONSTRINGDATABASE_DATABASENAME
Add keyed SQL client
Section titled “Add keyed SQL client”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.
Configuration
Section titled “Configuration”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.
Use a connection string
Section titled “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 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" }}Use configuration providers
Section titled “Use configuration providers”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 } } } }}Use inline delegates
Section titled “Use inline delegates”You can configure settings inline:
builder.AddSqlServerClient( "database", settings => settings.DisableHealthChecks = true);Observability and telemetry
Section titled “Observability and telemetry”Aspire integrations automatically set up Logging, Tracing, and Metrics configurations.
Logging
Section titled “Logging”The Aspire Azure SQL Database integration uses the following log categories:
Microsoft.Data.SqlClient
Tracing
Section titled “Tracing”The Aspire Azure SQL Database integration will emit the following tracing activities using OpenTelemetry:
Microsoft.Data.SqlClient
Metrics
Section titled “Metrics”The Aspire Azure SQL Database integration will emit the following metrics using OpenTelemetry:
Microsoft.Data.SqlClientdb.client.connections.create_timedb.client.connections.use_timedb.client.connections.wait_timedb.client.connections.idle.maxdb.client.connections.idle.mindb.client.connections.maxdb.client.connections.pending_requestsdb.client.connections.timeoutsdb.client.connections.usage