Visual Studio 2022 不断注入环境变量 —
ASPNETCORE_URLS
和 ASPNETCORE_HTTPS_PORT
— 我从不在我的 launchSettings.json
中声明这些变量。这与我的自定义 Kestrel 配置冲突。这是一个最小的控制台应用程序来证明这一点:
using System.Net;
namespace LaunchTrouble;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine($"ASPNETCORE_URLS - {Environment.GetEnvironmentVariable("ASPNETCORE_URLS")}");
Console.WriteLine($"ASPNETCORE_HTTPS_PORT - {Environment.GetEnvironmentVariable("ASPNETCORE_HTTPS_PORT")}");
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
// webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options =>
{
options.Listen(IPAddress.Any, 5111);
options.Listen(IPAddress.Any, 5222, listenOpts =>
{
listenOpts.UseHttps();
});
});
// Basic pipeline to avoid Startup overhead
webBuilder.Configure(_ => {});
})
.Build()
.Run();
}
}
这是我的launchSettings.json
:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"Dev": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
来自 Visual Studio 的日志(“Dev”配置文件中的 F5):
ASPNETCORE_URLS - https://localhost:5001/;http://localhost:5000/ <<?????
ASPNETCORE_HTTPS_PORT - 5001 <<?????
warn: Microsoft.AspNetCore.Server.Kestrel[0] <<?????
Overriding address(es) 'https://localhost:5001/, http://localhost:5000/'... <<?????
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://0.0.0.0:5111
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://0.0.0.0:5222
...
来自dotnet run --launch-profile Dev
的日志:
ASPNETCORE_URLS -
ASPNETCORE_HTTPS_PORT -
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://0.0.0.0:5111
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://0.0.0.0:5222
...
观察:
ASPNETCORE_URLS
被强制为 https://localhost:5001;http://localhost:5000 并且
ASPNETCORE_HTTPS_PORT
设置为 5001。
applicationUrl
中的所有
launchSettings.json
引用,但它仍然坚持推入 5001 和 5000。是否有一些隐藏的设置或技巧让 VS 只使用我在 Kestrel 中配置的端口?
到目前为止我发现的唯一解决方法是:
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": ""
}