These are my codes in AppHost
var postgres = builder
.AddPostgres("Postgres", port: 5432)
.WithImageTag("latest")
.WithContainerName("MyDatabase-PostgresDb-Aspire")
.WithEnvironment("POSTGRES_USER", "postgres")
.WithEnvironment("POSTGRES_PASSWORD", "postgrespassword")
.WithEnvironment("POSTGRES_DB", "MyDatabase")
.WithEnvironment("POSTGRES_HOST_AUTH_METHOD", "trust")
.WithDataBindMount(source: "F:\\DockerVolumes\\Postgres\\Data", isReadOnly: false)
.WithLifetime(ContainerLifetime.Persistent)
.AddDatabase("MyDatabase");
var web = builder.AddProject<ArshidWeb>(nameof(ArshidWeb))
.WithExternalHttpEndpoints()
.WithScalar().WithSwagger().WithOpenApi()
.WaitFor(postgres)
.WithReference(postgres)
;
And This is my ConnectionString
"ConnectionStrings": {
"PostgresConnection":"Host=Postgres;Port=5432;Database=MyDatabase;Username=postgres;Password=postgrespassword;"
}
It works with docker-compose.yml and dockerfile well
But with Aspire seems service discovery won't find Postgres server and throws this error :
System.Net.Sockets.SocketException: 'No such host is known.'
Where did I had mistake ?
These are my codes in AppHost
var postgres = builder
.AddPostgres("Postgres", port: 5432)
.WithImageTag("latest")
.WithContainerName("MyDatabase-PostgresDb-Aspire")
.WithEnvironment("POSTGRES_USER", "postgres")
.WithEnvironment("POSTGRES_PASSWORD", "postgrespassword")
.WithEnvironment("POSTGRES_DB", "MyDatabase")
.WithEnvironment("POSTGRES_HOST_AUTH_METHOD", "trust")
.WithDataBindMount(source: "F:\\DockerVolumes\\Postgres\\Data", isReadOnly: false)
.WithLifetime(ContainerLifetime.Persistent)
.AddDatabase("MyDatabase");
var web = builder.AddProject<ArshidWeb>(nameof(ArshidWeb))
.WithExternalHttpEndpoints()
.WithScalar().WithSwagger().WithOpenApi()
.WaitFor(postgres)
.WithReference(postgres)
;
And This is my ConnectionString
"ConnectionStrings": {
"PostgresConnection":"Host=Postgres;Port=5432;Database=MyDatabase;Username=postgres;Password=postgrespassword;"
}
It works with docker-compose.yml and dockerfile well
But with Aspire seems service discovery won't find Postgres server and throws this error :
System.Net.Sockets.SocketException: 'No such host is known.'
Where did I had mistake ?
As dear @JeffFritz's (Microsoft MVP) quote :
The name of ConnectionString have to be same with the name that is specified in AppHost
In ConnectionString name of Host have to be same with the name that is specified for container name (Postgres)
Now that works for both .Net Aspire and Docker
"ConnectionStrings": {
"MyDatabase":"Host=Postgres;Port=5432;Database=MyDatabase;Username=postgres;Password=postgrespassword;"
}
var postgres = builder
.AddPostgres("Postgres", port: 5432)
.AddDatabase("MyDatabase");