最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - .Net Aspire 9.1 - Service Discovery does not find Postgres server - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

As dear @JeffFritz's (Microsoft MVP) quote :

  1. The name of ConnectionString have to be same with the name that is specified in AppHost

  2. 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");
发布评论

评论列表(0)

  1. 暂无评论