我正在使用 ASP.NET Core 1 RTM Web 应用程序,并且正在将 Kestrel 设置更新为最新约定。安装程序旨在为 提供以下来源server.urls
,从最低到最高优先级:
- 在代码中设置的 URL
Program.Main()
(默认,例如用于生产) - 设置的 URL
hosting.Development.json
(例如,在开发时覆盖默认值) - 在环境变量中设置的 URL(例如,覆盖暂存或其他生产环境的默认值。)
根据最新的参考资料(例如在 SO和Github 上),这就是我现在得到的:
ProjDir\Program.cs
:
public class Program
{
// Entry point for the application
public static void Main(string[] args)
{
const string hostingDevFilepath = "hosting.Development.json";
const string environmentVariablesPrefix = "ASPNETCORE_";
string currentPath = Directory.GetCurrentDirectory();
var hostingConfig = new ConfigurationBuilder()
.SetBasePath(currentPath)
.AddJsonFile(hostingDevFilepath, optional: true)
.AddEnvironmentVariables(environmentVariablesPrefix)
.Build();
System.Console.WriteLine("From hostingConfig: " +
hostingConfig.GetSection("server.urls").Value);
var host = new WebHostBuilder()
.UseUrls("https://0.0.0.0")
.UseConfiguration(hostingConfig)
.UseKestrel()
.UseContentRoot(currentPath)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
ProjDir\hosting.Development.json
:
{
"server.urls": "http://localhost:51254"
}
从命令行,设置ASPNETCORE_ENVIRONMENT=Development
,这是输出:
> dotnet run
Project Root (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
From hostingConfig: http://localhost:51254
info: AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware[0]
An existing key was automatically added to the signing credentials list: <<yadda yadda yadda>>
Hosting environment: Development
Content root path: <<my project root dir>>
Now listening on: https://0.0.0.0:443
Application started. Press Ctrl+C to shut down.
我的预期输出将是Now listening on: http://localhost:51254
. UseConfiguration
URLs 值是从 JSON 源正确获取的(根据控制台日志),但是 Kestrel 配置会忽略它,即使在 UseUrls
.
我错过了什么?感谢您的建议。