Json Kestrel 设置中忽略了配置源
Json configuration source ignored in Kestrel setup
我正在使用 ASP.NET Core 1 RTM 网络应用程序,并且正在将 Kestrel 设置更新为最新的约定。安装程序旨在为 server.urls
提供以下来源,优先级从低到高:
- 在
Program.Main()
代码中设置的 URL(默认,例如用于生产)
hosting.Development.json
中设置的 URL(例如,在开发时覆盖默认值)
- 在环境变量中设置的 URL(例如覆盖暂存或其他生产环境的默认值)
根据最新的参考文献(例如 and 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
。
URL 值是从 JSON 源中正确获取的(根据控制台日志),但是 Kestrel 配置会忽略它,即使 UseConfiguration
在 之后 UseUrls
.
我错过了什么?感谢您的建议。
做了更多的测试。在我看来,一旦 UseUrls()
出现,无论顺序如何,所有 Json 配置源都会被忽略。
所以我试图提出一种支持多个 hosting.json
文件的解决方案,例如默认一个,然后每个环境一个。基本上我试图在 Program.Main()
中复制一种类似于 Startup.Startup(IHostingEnvironment env)
的行为,其中可以同时使用 "appsettings.json"
和 $"appsettings.{hostingEnv.EnvironmentName}.json"
作为源。唯一的问题是 Program.Main() 中没有可用的 IHostingEnvironment,但是 this GH issue 提醒我我们还有Environment.GetEnvironmentVariable("some-variable")
在我们的工具箱中。
这是完整的解决方案,请随时提出改进建议或(甚至更好)一些简化:
public class Program
{
// Entry point for the application
public static void Main(string[] args)
{
const string environmentVariablesPrefix = "ASPNETCORE_";
string hostingEnvironmentKey = $"{environmentVariablesPrefix}ENVIRONMENT";
string hostingEnvironmentValue;
try
{
hostingEnvironmentValue = Environment
.GetEnvironmentVariable(hostingEnvironmentKey);
}
catch
{
hostingEnvironmentValue = "Development";
}
const string hostingFilepath = "hosting.json";
string envHostingFilepath = $"hosting.{hostingEnvironmentValue}.json";
string currentPath = Directory.GetCurrentDirectory();
var hostingConfig = new ConfigurationBuilder()
.SetBasePath(currentPath)
.AddJsonFile(hostingFilepath, optional: true)
.AddJsonFile(envHostingFilepath, optional: true)
.AddEnvironmentVariables(environmentVariablesPrefix)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(hostingConfig)
.UseKestrel()
.UseContentRoot(currentPath)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
// in hosting.json
{
"server.urls": "https://0.0.0.0"
}
// in hosting.Development.json
{
"server.urls": "http://localhost:51254"
}
尝试使用 urls
而不是 server.urls
。设置的名称已更改 post RC2。
我正在使用 ASP.NET Core 1 RTM 网络应用程序,并且正在将 Kestrel 设置更新为最新的约定。安装程序旨在为 server.urls
提供以下来源,优先级从低到高:
- 在
Program.Main()
代码中设置的 URL(默认,例如用于生产) hosting.Development.json
中设置的 URL(例如,在开发时覆盖默认值)- 在环境变量中设置的 URL(例如覆盖暂存或其他生产环境的默认值)
根据最新的参考文献(例如
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
。
URL 值是从 JSON 源中正确获取的(根据控制台日志),但是 Kestrel 配置会忽略它,即使 UseConfiguration
在 之后 UseUrls
.
我错过了什么?感谢您的建议。
做了更多的测试。在我看来,一旦 UseUrls()
出现,无论顺序如何,所有 Json 配置源都会被忽略。
所以我试图提出一种支持多个 hosting.json
文件的解决方案,例如默认一个,然后每个环境一个。基本上我试图在 Program.Main()
中复制一种类似于 Startup.Startup(IHostingEnvironment env)
的行为,其中可以同时使用 "appsettings.json"
和 $"appsettings.{hostingEnv.EnvironmentName}.json"
作为源。唯一的问题是 Program.Main() 中没有可用的 IHostingEnvironment,但是 this GH issue 提醒我我们还有Environment.GetEnvironmentVariable("some-variable")
在我们的工具箱中。
这是完整的解决方案,请随时提出改进建议或(甚至更好)一些简化:
public class Program
{
// Entry point for the application
public static void Main(string[] args)
{
const string environmentVariablesPrefix = "ASPNETCORE_";
string hostingEnvironmentKey = $"{environmentVariablesPrefix}ENVIRONMENT";
string hostingEnvironmentValue;
try
{
hostingEnvironmentValue = Environment
.GetEnvironmentVariable(hostingEnvironmentKey);
}
catch
{
hostingEnvironmentValue = "Development";
}
const string hostingFilepath = "hosting.json";
string envHostingFilepath = $"hosting.{hostingEnvironmentValue}.json";
string currentPath = Directory.GetCurrentDirectory();
var hostingConfig = new ConfigurationBuilder()
.SetBasePath(currentPath)
.AddJsonFile(hostingFilepath, optional: true)
.AddJsonFile(envHostingFilepath, optional: true)
.AddEnvironmentVariables(environmentVariablesPrefix)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(hostingConfig)
.UseKestrel()
.UseContentRoot(currentPath)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
// in hosting.json
{
"server.urls": "https://0.0.0.0"
}
// in hosting.Development.json
{
"server.urls": "http://localhost:51254"
}
尝试使用 urls
而不是 server.urls
。设置的名称已更改 post RC2。