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 ?
Share Improve this question asked Mar 4 at 13:08 Arash.ZandiArash.Zandi 1,6372 gold badges22 silver badges33 bronze badges 2- 1 Did you write that connectionstring? .NET Aspire will provide you a connectionstring on the environment variable named "ConnectionStrings__MyDatabase". It uses the name "MyDatabase" from the name of the database instance you specified in your AppHost – Jeff Fritz Commented Mar 17 at 13:34
- Thanks @JeffFritz you are right I changed name of connection string to the name of database that I specified in apphost now it works fine. By the way could you please look at this and share your idea about that please ? nuget./packages/Arshid.Aspire.ApiDocs.Extensions github/ZandiArash/Arshid-Aspire-ApiDocs-Extensions – Arash.Zandi Commented Mar 18 at 6:11
1 Answer
Reset to default 0As 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");