Aspire.Hosting.SqlServer adds SQL Server resources to an Aspire host. Create a server and database, then reference the database from the API service. The API can use Aspire’s SQL Server client integration or keep its existing connection-string configuration. section 10.3
Listing 10.6 Full Aspire host with a SQL Server integration
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql").WithLifetime(ContainerLifetime.Persistent);
var sqldb = sql.AddDatabase("sqldb");
var apiService = builder.AddProject<Projects.OnlineShop_ApiService>("apiservice")
.WithHttpHealthCheck("/health")
.WaitFor(sqldb)
.WithReference(sqldb);
builder
.AddProject<Projects.OnlineShop_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithHttpHealthCheck("/health")
.WithReference(apiService)
.WaitFor(apiService);
builder.Build().Run();
A complete host example wires a persistent SQL Server resource and database to the API, then connects the web frontend to the API. section 10.3
