Aspire 9服务发现无法显示正确IP端点问题求助
已将Web项目配置为使用本机IP(192.168.0.2)并创建HTTPS证书,直接访问https://192.168.0.2:7058/一切正常,配置代码如下:
builder.WebHost.ConfigureKestrel(serverOptions => { serverOptions.Listen(IPAddress.Parse("192.168.0.2"), 7058, listenOptions => { listenOptions.UseHttps(@"d:\\aspire.pfx", "develPassword"); }); serverOptions.ListenAnyIP(7508, listenOptions => { listenOptions.UseHttps(@"d:\\aspire.pfx", "develPassword"); }); });
但在Aspire AppHost中无法显示正确的HTTP/HTTPS端点,反而显示http://localhost等无效地址,是否遗漏了某些配置?
附Web控制台日志:
2025-07-18T11:41:50 Waiting for resource 'apiservice' to enter the 'Running' state.
2025-07-18T11:41:50 Finished waiting for resource 'apiservice'.
2025-07-18T11:41:52 info: Yarp.ReverseProxy.Configuration.ConfigProvider.ConfigurationConfigProvider[1]
2025-07-18T11:41:52 Loading proxy data from config.
2025-07-18T11:41:52 warn: Microsoft.AspNetCore.Server.Kestrel[0]
2025-07-18T11:41:52 Overriding address(es) 'https://localhost:58040, http://localhost:58041, https://localhost:7058, https://localhost:7508';. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
2025-07-18T11:41:52 info: Microsoft.Hosting.Lifetime[14]
2025-07-18T11:41:52 Now listening on: https://192.168.0.2:7058
2025-07-18T11:41:52 info: Microsoft.Hosting.Lifetime[14]
2025-07-18T11:41:52 Now listening on: https://[::]:7508
2025-07-18T11:41:52 info: Microsoft.Hosting.Lifetime[0]
2025-07-18T11:41:52 Application started. Press Ctrl+C to shut down.
2025-07-18T11:41:52 info: Microsoft.Hosting.Lifetime[0]
2025-07-18T11:41:52 Hosting environment: Development
2025-07-18T11:41:52 info: Microsoft.Hosting.Lifetime[0]
2025-07-18T11:41:52 Content root path: C:\Progetti\net\CATest\CATest.Web
从日志警告能看出,Aspire AppHost默认会给服务分配localhost端口,和你手动配置的Kestrel端点冲突了。要让AppHost显示正确端点,按以下步骤操作:
1. 在AppHost里指定服务固定端点
打开AppHost的Program.cs,注册Web项目时用WithEndpoint方法明确指定你配置的HTTPS端点,同时禁用Aspire自动端口分配:
var builder = DistributedApplication.CreateBuilder(args); var webApp = builder.AddProject<Projects.CATest_Web>("webapp") .WithEndpoint(host: "192.168.0.2", port: 7058, scheme: "https") .WithEndpoint(host: "*", port: 7508, scheme: "https"); // 对应ListenAnyIP的7508端口 builder.Build().Run();
2. 清理launchSettings.json的默认地址
打开Web项目的Properties/launchSettings.json,删掉applicationUrl字段里的localhost地址,避免Kestrel加载时冲突:
{ "profiles": { "http": { "commandName": "Project" // 移除或注释原有的applicationUrl配置 }, "https": { "commandName": "Project" // 移除或注释原有的applicationUrl配置 } } }
3. 配置服务元数据(可选)
如果要让AppHost仪表板正确识别端点,可添加端点元数据,确保Aspire能解析服务对外地址:
var webApp = builder.AddProject<Projects.CATest_Web>("webapp") .WithEndpoint(7058, scheme: "https", host: "192.168.0.2", name: "primary-https") .WithEndpoint(7508, scheme: "https", host: "*", name: "anyip-https") .WithMetadata(new EndpointMetadata( new Uri("https://192.168.0.2:7058"), endpointName: "primary-https" ));
重启AppHost后,仪表板就会显示你配置的https://192.168.0.2:7058和https://[::]:7508端点,不再是localhost的无效地址。
内容的提问来源于stack exchange,提问作者Denis De Pauli

