-
Couldn't load subscription status.
- Fork 713
Description
Update: Turns out standard SQL Server now works on Mac with Appel Silicon, but the problem is exactly the same.
I've configured a .NET API and Azure SQL Edge SQL Server using Aspire, and the connection specifically fails when initiated from an Aspire Host. Here is the exception:
Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - 20)
It took me forever to isolate, but it turns out that this only happens when the API is started by the Aspire Host. I've created a Git repository that showcases the problem here: https://github.com/tjementum/AspireWithSqlServer, with even more info in the README.
If the same connection is initiated by starting the .NET API directly, everything works correctly. In fact, if I start everything using Aspire and let it fail, and then manually start the API it works. The repository has a Gif that shows the problem.
Here is a summary.
AppHost.Program.cs:
using Projects;
var builder = DistributedApplication.CreateBuilder(args);
var sqlPassword = "YourSecretPassword01!";
var sqlServer = builder.AddSqlServerContainer("localhost", sqlPassword, 1433)
.WithAnnotation(
new ContainerImageAnnotation { Registry = "mcr.microsoft.com", Image = "azure-sql-edge", Tag = "latest" }
);
builder.AddProject<AspireWithSqlServer_WebApi>("WebApi")
.WithEnvironment("SQL_PASSWORD", sqlPassword)
.WithEnvironment("SQL_SERVER", sqlServer.Resource.Name);
builder.Build().Run();Update: Turns out standard SQL Server now works on Mac with Appel Silicon, but the problem is exactly the same. I've updated this issue.
In the API, I connect to the database using Entity Framework with this connection string:
public static string GetConnectionString(this IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("Default");
var sqlServer = Environment.GetEnvironmentVariable("SQL_SERVER") ?? "localhost";
var sqlPassword = Environment.GetEnvironmentVariable("SQL_PASSWORD") ?? "YourSecretPassword01!";
return connectionString!
.Replace("${SQL_SERVER}", sqlServer)
.Replace("${SQL_PASSWORD}", sqlPassword);
}appsettings.json:
{
"ConnectionStrings": {
"Default": "Server=${SQL_SERVER};Database=weather;User Id=sa;Password=${SQL_PASSWORD};Encrypt=False;TrustServerCertificate=True"
}
}I'm running this on a MacBook M1 with an ARM processor , which necessitates the use of Azure SQL Edge, as the standard SQL Server is not supported on ARM architectures.