【问题标题】:Json configuration source ignored in Kestrel setup在 Kestrel 设置中忽略了 Json 配置源
【发布时间】:2016-07-11 20:20:46
【问题描述】:

我正在使用 ASP.NET Core 1 RTM Web 应用程序,并且正在将 Kestrel 设置更新为最新约定。安装程序旨在为server.urls 提供以下来源,从最低到最高优先级:

  1. Program.Main() 代码中设置的网址(默认,例如用于生产)
  2. hosting.Development.json 中设置的 URL(例如,在开发时覆盖默认值)
  3. 在环境变量中设置的 URL(例如,覆盖暂存或其他生产环境的默认值。)

根据最新的参考资料(例如 here on SOhere 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


【解决方案1】:

尝试使用urls 而不是server.urls。设置名称在 RC2 后更改。

【讨论】:

  • 引用的更改属于标记为 1.0.0 的提交,因此可能是 RC2 后,登陆到 RTM
【解决方案2】:

做了更多的测试。在我看来,一旦出现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"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-31
    • 2011-05-09
    • 2016-05-15
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多