如何在启动类中配置Kestrel URL

Ste*_*eve 8 kestrel-http-server asp.net-core

我试图找出修改kestrel 从Startup类构造函数侦听的URL的正确方法.

更新:除了下面的答案,我已经明白Startup类不是以我想象的方式配置Kestrel.我原以为Startup会创建一个单独的应用程序范围的配置对象,它可以通过约定来定位,但事实并非如此.正如@Tseng所指出的,应用程序和托管的配置是另外一个问题.链接的答案和接受的答案提供了工作示例.

我在Vagrant中创建了一个全新的Ubuntu 14盒子,并根据Microsoft当前的说明安装了ASP.NET Core 1.1:https://www.microsoft.com/net/core#linuxubuntu

我跑了:

  • dotnet new -t web
  • dotnet恢复
  • dotnet运行

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

我真正想要的是通过在Startup类中添加一个新的JSON设置文件来按照惯例更改URL,所以我按照示例创建了一个hosting.JSON文件(注意:我已经尝试过"server.urls"和"网址"作为关键).

{
  urls: "http://*:5001"
}
Run Code Online (Sandbox Code Playgroud)

在Startup.cs下面添加appsettings.json文件的默认行我添加了.AddJsonFile("hosting.json",可选: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();
}
Run Code Online (Sandbox Code Playgroud)

我已经验证配置构建后设置是否存在,但是当在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();
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

基本上我想将Startup和Program分开,将一个hosting.JSON文件添加到带有URL的配置中,并将其拾取并使用,而无需调用UseUrls,UseConfiguration等.

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

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

  • 该帖特别说"launcherSettings适用于Visual Studio F5".我在Linux机器上使用命令行.与VS无关.

  • 该帖子中提供的解决方案将配置构建移动到main方法中.我特别声明我想在Startup类中构建我的配置,就像默认的"dotnet new -t web"项目一样.

我不认为这是重复的,我仍在审查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";
  }
}
Run Code Online (Sandbox Code Playgroud)

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";
Run Code Online (Sandbox Code Playgroud)

Anu*_*raj 6

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

这是我的hosting.json文件.

{
  "server.urls": "http://localhost:5010;http://localhost:5012"
}
Run Code Online (Sandbox Code Playgroud)

这是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();
}
Run Code Online (Sandbox Code Playgroud)

这是截图.

运行ASP.NET Core应用程序的屏幕截图

  • @Steve:因为这两种配置都是不相关的。一种用于您的应用程序,另一种仅与托管相关。他们只是碰巧使用相同的配置框架/库来实现它。您还可以使用命令行或环境变量来设置端口,所有方法都在链接的**答案**(不是帖子)中 (2认同)
  • `.AddCommandLine(args)`应该在`.AddJsonFile("hosting.json",可选:true)之后,否则你不能用控制台args覆盖配置. (2认同)