如何在启动时配置 Kestrel url class

How to configure Kestrel urls in startup class

我正在尝试找出修改 kestrel 在 上从 Startup class 构造函数监听的 URL(s) 的正确方法.

更新:除了下面的答案之外,我开始明白 Startup class 并没有按照我想的那样用于配置 Kestrel .我原以为 Startup 会创建一个单一的应用程序范围的配置对象,该对象将通过约定进行定位,但事实并非如此。正如@Tseng 指出的那样,应用程序和托管的配置是不同的问题。链接的答案和接受的答案提供了工作示例。

我在 Vag运行t 中启动了一个 b运行d new Ubuntu 14 box,并根据 Microsoft 的当前说明安装了 ASP.NET Core 1.1:https://www.microsoft.com/net/core#linuxubuntu

我运行:

这默认在 http://localhost:5000 上侦听。在 Program.cs 中,我可以调用 app.UseUrls("http://*:5001),这可以更改 URL 和端口。

我真正想要的是通过在启动 class 中添加一个新的 JSON 设置文件来按照惯例更改 URLs,所以我按照示例创建了一个 hosting.JSON 文件(注意:我试过 "server.urls" 和 "urls" 作为键)。

{
  urls: "http://*:5001"
}

在用于添加 appsettings.json 文件的默认行下方的 Startup.cs 中,我添加了 .AddJsonFile("hosting.json", optional: false); (可选:false 以确保它正在拾取文件)

public Startup(IHostingEnvironment env)
{
  var builder = new ConfigurationBuilder()
  .SetBasePath(env.ContentRootPath)
  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
  .AddJsonFile("hosting.json", optional: false);
  if (env.IsDevelopment())
  {
    // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
    builder.AddUserSecrets();
  }

  builder.AddEnvironmentVariables();
  Configuration = builder.Build();
}

我已验证设置在构建配置后存在,但在 Program.cs

中构建主机时未被拾取或使用
public class Program
{
  public static void Main(string[] args)
  {
    var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .Build();

    host.Run();
  }
}

并且应用程序仍在 localhost:5000 上开始侦听。我(可能不正确)的理解是,通过使用正确的密钥正确命名设置,WebHostBuidler 应该选择并使用它。

我看到其他示例基本上摆脱了启动 class 并在 Program.cs 中创建配置,然后可以将其传递到调用 UseConfiguration 但我的理解是,使用 Startup class 应该按照惯例执行此操作。

基本上我想将启动和程序分开,在配置中添加一个hosting.JSON文件URLs,并在不需要调用 UseUrls、UseConfiguration 等的情况下获取和使用它

我是不是遗漏了一些明显的东西,或者试图做一些实际上不正确的事情?

根据我的评论解释为什么这不是重复的:

我不认为这是重复的,并且仍在审查 ASP.NET 核心来源以查看是否可行。

关于正确的密钥:

https://github.com/aspnet/Hosting/blob/b6da89f54cff11474f17486cdc55c2f21f2bbd6b/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs

namespace Microsoft.AspNetCore.Hosting
{
  public static class WebHostDefaults
  {
    public static readonly string ApplicationKey = "applicationName";
    public static readonly string StartupAssemblyKey = "startupAssembly";
    public static readonly string DetailedErrorsKey = "detailedErrors";
    public static readonly string EnvironmentKey = "environment";
    public static readonly string WebRootKey = "webroot";
    public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
    public static readonly string ServerUrlsKey = "urls";
    public static readonly string ContentRootKey = "contentRoot";
  }
}

https://github.com/aspnet/Hosting/blob/80ae7f056c08b740820ee42a7df9eae34541e49e/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs

public class WebHost : IWebHost
{
  private static readonly string DeprecatedServerUrlsKey = "server.urls";

在您的 hosting.json 文件中,您应该使用 server.urls 而不是 url。并且 hosting.json 文件需要在 program.cs(在 main 方法中)而不是在启动时添加。

这是我的 hosting.json 文件。

{
  "server.urls": "http://localhost:5010;http://localhost:5012"
}

这是 Main 方法。

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddCommandLine(args)
        .AddEnvironmentVariables(prefix: "ASPNETCORE_")
        .AddJsonFile("hosting.json", optional: true)
        .Build();

    var host = new WebHostBuilder()
        .UseConfiguration(config)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

这是屏幕截图。