|
| 1 | +--- |
| 2 | +ms.topic: include |
| 3 | +--- |
| 4 | + |
| 5 | +To get started with the .NET Aspire SQL Server client integration, install the [📦 Aspire.Microsoft.Data.SqlClient](https://www.nuget.org/packages/Aspire.Microsoft.Data.SqlClient) NuGet package in the client-consuming project, that is, the project for the application that uses the SQL Server client. The SQL Server client integration registers a <xref:System.Data.SqlClient.SqlConnection> instance that you can use to interact with SQL Server. |
| 6 | + |
| 7 | +### [.NET CLI](#tab/dotnet-cli) |
| 8 | + |
| 9 | +```dotnetcli |
| 10 | +dotnet add package Aspire.Microsoft.Data.SqlClient |
| 11 | +``` |
| 12 | + |
| 13 | +### [PackageReference](#tab/package-reference) |
| 14 | + |
| 15 | +```xml |
| 16 | +<PackageReference Include="Aspire.Microsoft.Data.SqlClient" |
| 17 | + Version="*" /> |
| 18 | +``` |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +### Add SQL Server client |
| 23 | + |
| 24 | +In the _:::no-loc text="Program.cs":::_ file of your client-consuming project, call the <xref:Microsoft.Extensions.Hosting.AspireSqlServerSqlClientExtensions.AddSqlServerClient*> extension method on any <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder> to register a `SqlConnection` for use via the dependency injection container. The method takes a connection name parameter. |
| 25 | + |
| 26 | +```csharp |
| 27 | +builder.AddSqlServerClient(connectionName: "database"); |
| 28 | +``` |
| 29 | + |
| 30 | +> [!TIP] |
| 31 | +> The `connectionName` parameter must match the name used when adding the SQL Server database resource in the app host project. In other words, when you call `AddDatabase` and provide a name of `database` that same name should be used when calling `AddSqlServerClient`. For more information, see [Add SQL Server resource and database resource](../sql-server-integration.md#add-sql-server-resource-and-database-resource). |
| 32 | +
|
| 33 | +You can then retrieve the <xref:Microsoft.Data.SqlClient.SqlConnection> instance using dependency injection. For example, to retrieve the connection from an example service: |
| 34 | + |
| 35 | +```csharp |
| 36 | +public class ExampleService(SqlConnection connection) |
| 37 | +{ |
| 38 | + // Use connection... |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +For more information on dependency injection, see [.NET dependency injection](/dotnet/core/extensions/dependency-injection). |
| 43 | + |
| 44 | +### Add keyed SQL Server client |
| 45 | + |
| 46 | +There might be situations where you want to register multiple `SqlConnection` instances with different connection names. To register keyed SQL Server clients, call the <xref:Microsoft.Extensions.Hosting.AspireSqlServerSqlClientExtensions.AddKeyedSqlServerClient*> method: |
| 47 | + |
| 48 | +```csharp |
| 49 | +builder.AddKeyedSqlServerClient(name: "mainDb"); |
| 50 | +builder.AddKeyedSqlServerClient(name: "loggingDb"); |
| 51 | +``` |
| 52 | + |
| 53 | +> [!IMPORTANT] |
| 54 | +> When using keyed services, it's expected that your SQL Server resource configured two named databases, one for the `mainDb` and one for the `loggingDb`. |
| 55 | +
|
| 56 | +Then you can retrieve the `SqlConnection` instances using dependency injection. For example, to retrieve the connection from an example service: |
| 57 | + |
| 58 | +```csharp |
| 59 | +public class ExampleService( |
| 60 | + [FromKeyedServices("mainDb")] SqlConnection mainDbConnection, |
| 61 | + [FromKeyedServices("loggingDb")] SqlConnection loggingDbConnection) |
| 62 | +{ |
| 63 | + // Use connections... |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +For more information on keyed services, see [.NET dependency injection: Keyed services](/dotnet/core/extensions/dependency-injection#keyed-services). |
| 68 | + |
| 69 | +### Configuration |
| 70 | + |
| 71 | +The .NET Aspire SQL Server integration provides multiple options to configure the connection based on the requirements and conventions of your project. |
| 72 | + |
| 73 | +#### Use a connection string |
| 74 | + |
| 75 | +When using a connection string from the `ConnectionStrings` configuration section, you can provide the name of the connection string when calling the <xref:Microsoft.Extensions.Hosting.AspireSqlServerSqlClientExtensions.AddSqlServerClient*> method: |
| 76 | + |
| 77 | +```csharp |
| 78 | +builder.AddSqlServerClient(connectionName: "sql"); |
| 79 | +``` |
| 80 | + |
| 81 | +Then the connection string is retrieved from the `ConnectionStrings` configuration section: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "ConnectionStrings": { |
| 86 | + "database": "Data Source=myserver;Initial Catalog=master" |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +For more information on how to format this connection string, see the [ConnectionString](/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring#remarks). |
| 92 | + |
| 93 | +#### Use configuration providers |
| 94 | + |
| 95 | +The .NET Aspire SQL Server integration supports <xref:Microsoft.Extensions.Configuration>. It loads the <xref:Aspire.Microsoft.Data.SqlClient.MicrosoftDataSqlClientSettings> from configuration by using the `Aspire:Microsoft:Data:SqlClient` key. The following snippet is an example of a _:::no-loc text="appsettings.json":::_ file that configures some of the options: |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "Aspire": { |
| 100 | + "Microsoft": { |
| 101 | + "Data": { |
| 102 | + "SqlClient": { |
| 103 | + "ConnectionString": "YOUR_CONNECTIONSTRING", |
| 104 | + "DisableHealthChecks": false, |
| 105 | + "DisableMetrics": true |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +For the complete SQL Server client integration JSON schema, see [Aspire.Microsoft.Data.SqlClient/ConfigurationSchema.json](https://github.com/dotnet/aspire/blob/v8.2.2/src/Components/Aspire.Microsoft.Data.SqlClient/ConfigurationSchema.json). |
| 114 | + |
| 115 | +#### Use inline delegates |
| 116 | + |
| 117 | +Also you can pass the `Action<MicrosoftDataSqlClientSettings> configureSettings` delegate to set up some or all the options inline, for example to disable health checks from code: |
| 118 | + |
| 119 | +```csharp |
| 120 | +builder.AddSqlServerClient( |
| 121 | + "database", |
| 122 | + static settings => settings.DisableHealthChecks = true); |
| 123 | +``` |
| 124 | + |
| 125 | +### Client integration health checks |
| 126 | + |
| 127 | +By default, .NET Aspire integrations enable [health checks](../../fundamentals/health-checks.md) for all services. For more information, see [.NET Aspire integrations overview](../../fundamentals/integrations-overview.md). |
| 128 | + |
| 129 | +The .NET Aspire SQL Server integration: |
| 130 | + |
| 131 | +- Adds the health check when <xref:Aspire.Microsoft.Data.SqlClient.MicrosoftDataSqlClientSettings.DisableHealthChecks?displayProperty=nameWithType> is `false`, which attempts to connect to the SQL Server. |
| 132 | +- Integrates with the `/health` HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic. |
| 133 | + |
| 134 | +[!INCLUDE [integration-observability-and-telemetry](../../includes/integration-observability-and-telemetry.md)] |
| 135 | + |
| 136 | +#### Logging |
| 137 | + |
| 138 | +The .NET Aspire SQL Server integration currently doesn't enable logging by default due to limitations of the <xref:Microsoft.Data.SqlClient>. |
| 139 | + |
| 140 | +#### Tracing |
| 141 | + |
| 142 | +The .NET Aspire SQL Server integration emits the following tracing activities using OpenTelemetry: |
| 143 | + |
| 144 | +- `OpenTelemetry.Instrumentation.SqlClient` |
| 145 | + |
| 146 | +#### Metrics |
| 147 | + |
| 148 | +The .NET Aspire SQL Server integration will emit the following metrics using OpenTelemetry: |
| 149 | + |
| 150 | +- Microsoft.Data.SqlClient.EventSource |
| 151 | + - `active-hard-connections` |
| 152 | + - `hard-connects` |
| 153 | + - `hard-disconnects` |
| 154 | + - `active-soft-connects` |
| 155 | + - `soft-connects` |
| 156 | + - `soft-disconnects` |
| 157 | + - `number-of-non-pooled-connections` |
| 158 | + - `number-of-pooled-connections` |
| 159 | + - `number-of-active-connection-pool-groups` |
| 160 | + - `number-of-inactive-connection-pool-groups` |
| 161 | + - `number-of-active-connection-pools` |
| 162 | + - `number-of-inactive-connection-pools` |
| 163 | + - `number-of-active-connections` |
| 164 | + - `number-of-free-connections` |
| 165 | + - `number-of-stasis-connections` |
| 166 | + - `number-of-reclaimed-connections` |
0 commit comments