Skip to main content
net8.0

A Microsoft SQL Server client that integrates with Aspire, including health checks, metrics and telemetry.

Published
Feb 13, 2024
2y ago
Total Downloads
2.0M
2,011,348

Deprecated Package Version

Reason: Other, Legacy

This version is out of support and is no longer maintained. Please upgrade to the latest version. See our support policy for details: https://aka.ms/dotent/aspire/support

Install

dotnet add package Aspire.Microsoft.Data.SqlClient --version 8.0.0-preview.3.24105.21
ProductVersions
.NET
net8.0

README

Aspire.Microsoft.Data.SqlClient library

Registers 'Scoped' Microsoft.Data.SqlClient.SqlConnection factory in the DI container for connecting Azure SQL, MS SQL database. Enables health check, metrics and telemetry.

Getting started

Prerequisites

  • Azure SQL or MS SQL server database and the connection string for accessing the database.

Install the package

Install the .NET Aspire SQL Server SqlClient library with NuGet:

dotnet add package Aspire.Microsoft.Data.SqlClient

Usage example

In the Program.cs file of your 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("sqldata");

You can then retrieve the SqlConnection instance using dependency injection. For example, to retrieve the connection from a Web API controller:

private readonly SqlConnection _connection;

public ProductsController(SqlConnection connection)
{
    _connection = connection;
}

Configuration

The .NET Aspire SqlClient component provides multiple options to configure the SQL connection based on the requirements and conventions of your project.

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 builder.AddSqlServerClient():

builder.AddSqlServerClient("myConnection");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "myConnection": "Data Source=myserver;Initial Catalog=master"
  }
}

See the ConnectionString documentation for more information on how to format this connection string.

Use configuration providers

The .NET Aspire SqlClient component supports Microsoft.Extensions.Configuration. It loads the MicrosoftDataSqlClientSettings from configuration by using the Aspire:Microsoft:Data:SqlClient key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Microsoft": {
      "Data": {
        "SqlClient": {
          "HealthChecks": true,
          "Metrics": false
        }
      }
    }
  }
}

Use inline delegates

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:

    builder.AddSqlServerClient("sqldata", settings => settings.HealthChecks = false);

AppHost extensions

In your AppHost project, register a SqlServer database and consume the connection using the following methods:

var sql = builder.AddSqlServer("sql").AddDatabase("sqldata");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(sql);

The WithReference method configures a connection in the MyService project named sqldata. In the Program.cs file of MyService, the sql connection can be consumed using:

builder.AddSqlServerClient("sqldata");

Additional documentation

Feedback & contributing

https://github.com/dotnet/aspire

Changes from Previous Version

No structural changes between v8.0.0-preview.2.23619.3 and v8.0.0-preview.3.24105.21.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.NuGet marks this package as deprecated. Review the publisher's migration guidance before adopting it.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.