如何配置.NET Aspire测试使用launchSettings.json指定端口?
在.NET Aspire测试中使用launchSettings.json定义的固定端口
我目前在使用.NET Aspire,所有项目都能正常运行,现在正在创建测试,希望确保每个项目运行在其launchSettings.json中定义的端口,而不是随机端口。当前的测试代码如下:
var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Aspire_AppHost>(); appHost.Services.ConfigureHttpClientDefaults(clientBuilder => { clientBuilder.AddStandardResilienceHandler(); }); var app = await appHost.BuildAsync(); await app.StartAsync(); var http = App.CreateHttpClient("myApi"); string url = http.BaseAddress.Authority;
现在url被分配了带随机端口的localhost,请问该如何配置测试设置以使用launchSettings.json中的正确端口?
解决方法
要让Aspire测试使用launchSettings.json里的固定端口,需要在测试代码中显式加载并应用launchSettings的配置,具体步骤如下:
1. 读取launchSettings.json配置
在测试项目中,添加代码读取目标项目的launchSettings.json文件,解析出对应的端口配置。假设你的API项目位于../MyApi目录下:
using System.Text.Json; // 读取目标项目的launchSettings.json var launchSettingsPath = Path.Combine(Directory.GetCurrentDirectory(), "../MyApi/Properties/launchSettings.json"); var launchSettingsJson = await File.ReadAllTextAsync(launchSettingsPath); var launchSettings = JsonDocument.Parse(launchSettingsJson).RootElement; // 提取指定配置的HTTPS端口(可根据需求切换为HTTP) var httpsPort = launchSettings.GetProperty("profiles") .GetProperty("MyApi") .GetProperty("applicationUrl") .GetString() ?.Split(';') .First(u => u.StartsWith("https://")) .Split(':') .Last();
2. 在Aspire测试中覆盖服务端口配置
通过DistributedApplicationTestingBuilder的配置,为目标服务指定固定端口:
var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Aspire_AppHost>(); // 为myApi服务配置固定端口 if (!string.IsNullOrEmpty(httpsPort)) { appHost.Services.Configure<ServiceEndpointOptions>("myApi", options => { options.Port = int.Parse(httpsPort); options.Scheme = "https"; }); } appHost.Services.ConfigureHttpClientDefaults(clientBuilder => { clientBuilder.AddStandardResilienceHandler(); }); var app = await appHost.BuildAsync(); await app.StartAsync(); var http = App.CreateHttpClient("myApi"); string url = http.BaseAddress.Authority; // 此时将使用launchSettings中定义的固定端口
3. 可选:封装通用读取方法
如果多个项目需要处理,可以封装一个通用方法来复用端口读取逻辑:
private static async Task<int> GetLaunchSettingsPort(string projectRelativePath, string profileName, string scheme = "https") { var launchSettingsPath = Path.Combine(Directory.GetCurrentDirectory(), projectRelativePath, "Properties/launchSettings.json"); var launchSettingsJson = await File.ReadAllTextAsync(launchSettingsPath); var launchSettings = JsonDocument.Parse(launchSettingsJson).RootElement; var appUrl = launchSettings.GetProperty("profiles") .GetProperty(profileName) .GetProperty("applicationUrl") .GetString(); if (string.IsNullOrEmpty(appUrl)) throw new InvalidOperationException("未找到有效的applicationUrl配置"); var targetUrl = appUrl.Split(';').First(u => u.StartsWith($"{scheme}://")); return int.Parse(targetUrl.Split(':').Last()); }
调用示例:
var apiPort = await GetLaunchSettingsPort("../MyApi", "MyApi"); appHost.Services.Configure<ServiceEndpointOptions>("myApi", options => { options.Port = apiPort; options.Scheme = "https"; });
内容的提问来源于stack exchange,提问作者man developer
相关产品推荐
相关产品推荐

