콘텐츠로 이동
Docs Try Aspire
Docs Try

Connect to Azure SQL Database

이 콘텐츠는 아직 번역되지 않았습니다.

Azure SQL Database logo

This page describes how consuming apps connect to an Azure SQL Database resource that’s already modeled in your AppHost. For the AppHost API surface — adding an Azure SQL server, databases, running as a local container, managed identity, and more — see Azure SQL Database Hosting integration.

When you reference an Azure SQL Database resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire SQL Server client integration for automatic dependency injection, health checks, and telemetry.

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

The Azure SQL server resource exposes the following connection properties:

Property NameDescription
HostThe fully qualified domain name of the Azure SQL Server
PortThe SQL Server port (1433 for Azure)
UriThe connection URI, with the format mssql://{Host}:{Port}
JdbcConnectionStringJDBC connection string, with the format jdbc:sqlserver://{Host}:{Port};encrypt=true;trustServerCertificate=false

Example connection strings:

Uri: mssql://myserver.database.windows.net:1433
JdbcConnectionString: jdbc:sqlserver://myserver.database.windows.net:1433;encrypt=true;trustServerCertificate=false

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

Property NameDescription
DatabaseNameThe name of the database
UriThe connection URI with the database name, with the format mssql://{Host}:{Port}/{DatabaseName}
JdbcConnectionStringJDBC connection string with the database name, with the format jdbc:sqlserver://{Host}:{Port};database={DatabaseName};encrypt=true;trustServerCertificate=false

Example connection strings:

Uri: mssql://myserver.database.windows.net:1433/mydb
JdbcConnectionString: jdbc:sqlserver://myserver.database.windows.net:1433;database=mydb;encrypt=true;trustServerCertificate=false

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure SQL database resource named database and references it from the consuming app.

For C# apps, the recommended approach is the Aspire SQL Server client integration. It registers a SqlConnection through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 Aspire.Microsoft.Data.SqlClient NuGet package in the client-consuming project:

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

In Program.cs, call AddSqlServerClient on your IHostApplicationBuilder to register a SqlConnection:

C# — Program.cs
builder.AddSqlServerClient(connectionName: "database");

Resolve the connection through dependency injection:

C# — ExampleService.cs
public class ExampleService(SqlConnection connection)
{
// Use connection...
}

To register multiple SqlConnection instances with different connection names, use AddKeyedSqlServerClient:

C# — Program.cs
builder.AddKeyedSqlServerClient(name: "primary-db");
builder.AddKeyedSqlServerClient(name: "secondary-db");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("primary-db")] SqlConnection primaryConnection,
[FromKeyedServices("secondary-db")] SqlConnection secondaryConnection)
{
// Use connections...
}

For more information on keyed services, see .NET dependency injection: Keyed services.

The Aspire SQL Server client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddSqlServerClient:

C# — Program.cs
builder.AddSqlServerClient("database");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"database": "Server=myserver.database.windows.net;Database=mydb;Authentication=Active Directory Default;Encrypt=True"
}
}

For more information on connection string format, see SqlConnection.ConnectionString.

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads MicrosoftDataSqlClientSettings from appsettings.json (or any other configuration source) by using the Aspire:Microsoft:Data:SqlClient key:

JSON — appsettings.json
{
"Aspire": {
"Microsoft": {
"Data": {
"SqlClient": {
"DisableHealthChecks": false,
"DisableTracing": false,
"DisableMetrics": false
}
}
}
}
}

Inline delegates. Pass an Action<MicrosoftDataSqlClientSettings> to configure settings inline, for example to disable health checks:

C# — Program.cs
builder.AddSqlServerClient(
"database",
static settings => settings.DisableHealthChecks = true);

Aspire client integrations enable health checks by default. The SQL Server client integration adds:

  • A health check that attempts to connect to the Azure SQL Database instance and execute a command.
  • Integration with the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire SQL Server client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging: The SQL Server client integration currently doesn’t enable logging by default due to limitations of the SqlClient.

Tracing activities:

  • OpenTelemetry.Instrumentation.SqlClient

Metrics from 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

Any of these telemetry features can be disabled through the configuration options above.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection URI from the environment and open a connection using 📦 Microsoft.Data.SqlClient:

C# — Program.cs
using Microsoft.Data.SqlClient;
var connectionString = Environment.GetEnvironmentVariable("DATABASE_URI");
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
// Use connection to query the database...