How to fix hostPort for SQL Server container in .NET Aspire

133 Views Asked by At

I want to fix the hostPort for SQL Server component so that I can use SSMS or any other tools to access the database. I don't want to update the port every time.

I was able to do that using 8.0.0-preview.1.23557.2 but I am not for the higher version. Aspire always sets a random port

https://learn.microsoft.com/en-us/dotnet/aspire/database/sql-server-component?tabs=dotnet-cli

The sample code using WithEndpoint

var builder = DistributedApplication.CreateBuilder(args);

var apiService = builder.AddProject<Projects.AspireApp1_ApiService>("apiservice");

var sql = builder.AddSqlServerContainer("sql")
    .AddDatabase("sqldata")
    .WithEndpoint(1433, 1433);

builder.AddProject<Projects.AspireApp1_Web>("webfrontend")      
    .WithReference(sql)
    .WithReference(apiService); 

builder.Build().Run();  // Run the distributed application

It works for the HTTP/HTTPS facing endpoints https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/networking-overview

0

There are 0 best solutions below