1

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

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

根据最新的参考资料(例如在 SOGithub 上),这就是我现在得到的:

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. UseConfigurationURLs 值是从 JSON 源正确获取的(根据控制台日志),但是 Kestrel 配置会忽略它,即使 UseUrls.

我错过了什么?感谢您的建议。

4

2 回答 2

2

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

于 2016-07-11T21:34:49.040 回答
1

又做了一些测试。在我看来,一旦UseUrls()出现,无论以何种顺序,所有 Json 配置源都会被忽略。

所以我试图想出一个支持多个hosting.json文件的解决方案,例如一个默认文件,然后每个环境一个。基本上,我尝试以Program.Main()类似于 的行为进行复制Startup.Startup(IHostingEnvironment env),其中可以同时使用"appsettings.json"$"appsettings.{hostingEnv.EnvironmentName}.json"作为源。唯一的问题是在Program.Main()中没有可用的IHostingEnvironment,但是这个 GH 问题提醒我,我们的工具带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"
}
于 2016-07-11T20:54:38.607 回答