Overview
TheAspire.Microsoft.Data.SqlClient component registers a scoped SqlConnection factory in your dependency injection container for connecting to Azure SQL and SQL Server databases. It automatically enables health checks, logging, and distributed tracing.
Installation
Install the component using the .NET CLI:Usage
Register the component
In your service’sProgram.cs file, call the AddSqlServerClient extension method:
Inject and use the connection
Retrieve theSqlConnection instance using dependency injection:
The
SqlConnection is registered as scoped, meaning a new connection is created per request in web applications.Configuration
The component provides multiple configuration options based on your project requirements.Connection string
Provide a connection string in yourappsettings.json:
Configuration providers
Configure component settings using theAspire:Microsoft:Data:SqlClient configuration section:
Inline configuration
Configure settings directly in code using a delegate:AppHost integration
In your AppHost project, install the SQL Server hosting package:Program.cs:
WithReference method automatically configures the connection string in your service. Consume it in your service’s Program.cs:
The connection name passed to
WithReference must match the name used in AddSqlServerClient.Configuration options
The following settings are available:| Setting | Description | Default |
|---|---|---|
DisableHealthChecks | Disable automatic health check registration | false |
DisableTracing | Disable OpenTelemetry tracing | false |
Health checks
The component automatically registers a health check that verifies the database connection. The health check:- Opens a connection to the database
- Executes a simple query to verify connectivity
- Reports the status in the
/healthendpoint
DisableHealthChecks to true.
Observability
Logging
The component integrates with .NET logging to provide:- Connection events
- Command execution information
- Error details
Tracing
Distributed tracing captures:- Database operations
- Query execution time
- Connection activity
Best practices
Use dependency injection
Use dependency injection
Always inject
SqlConnection through the constructor rather than creating connections manually. This ensures proper lifecycle management and integration with Aspire’s observability features.Use parameterized queries
Use parameterized queries
Protect against SQL injection by using parameterized queries:
Handle connection state
Handle connection state
Check the connection state before executing commands:
Use async methods
Use async methods
Always use async methods for database operations to avoid blocking threads:
Dispose resources properly
Dispose resources properly
Use
using statements to ensure proper disposal of commands and readers:Common scenarios
Azure SQL with managed identity
For Azure SQL databases, use managed identity for authentication:Connection resilience
Add connection resiliency for production environments:Additional resources
Related components
PostgreSQL
Connect to PostgreSQL databases
MongoDB
Work with MongoDB databases
Azure Cosmos DB
Connect to Azure Cosmos DB
SQL Server Hosting
Learn about the SQL Server hosting integration