【发布时间】:2016-07-11 20:20:46
【问题描述】:
我正在使用 ASP.NET Core 1 RTM Web 应用程序,并且正在将 Kestrel 设置更新为最新约定。安装程序旨在为server.urls 提供以下来源,从最低到最高优先级:
- 在
Program.Main()代码中设置的网址(默认,例如用于生产) -
hosting.Development.json中设置的 URL(例如,在开发时覆盖默认值) - 在环境变量中设置的 URL(例如,覆盖暂存或其他生产环境的默认值。)
根据最新的参考资料(例如 here on SO 和 here on 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。
URLs 值是从 JSON 源正确获取的(根据控制台日志),但 Kestrel 配置会忽略这一点,即使 UseConfiguration 在 UseUrls 之后出现。
我错过了什么?感谢您的建议。
【问题讨论】:
-
如果您使用
urls作为名称而不是server.urls会怎样? (github.com/aspnet/Hosting/blob/…) -
我刚刚尝试(重新)添加两个
UseUrls(),然后在 Json 源中设置urls。来自 Json 源的设置被正确拾取!两个问题? Q1:这是否记录在某个地方(除了查看代码:)) Q2:如果没有UseUrls,那么即使server.urls也可以工作? Tnx -
我相信如果它被配置它会被自动拾取。
UseUrls允许您以编程方式配置 url。 -
是的,我明白了。这是我不知道的 new 设置名称。听起来很奇怪 UseUrls 适用于新的而不是旧的。但话说回来,这只是来自 RC ......请随时回答这个问题。
-
我认为它在 RC1 之后发生了变化
标签: asp.net-core kestrel-http-server