【问题标题】:Cannot change Kestrel listening port from command line无法从命令行更改 Kestrel 侦听端口
【发布时间】:2016-08-23 10:35:16
【问题描述】:

我有以下入口点:

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

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

    host.Run();
}

如果我添加类似的 hosts.json 文件,它会起作用

{
  "server.urls": "http://0.0.0.0:5001"
}

或者如果我定义环境变量(找到名称here

SET ASPNETCORE_URLS=https://0.0.0.0:5001

但是如果我将--server.urls http://0.0.0.0:5001作为参数传递,应用程序会监听默认的5000端口:

> dotnet run --server.urls http://0.0.0.0:5001
...
Now listening on: http://localhost:5000

【问题讨论】:

  • 您使用的是哪个版本的 dotnet?
  • @DirkVollmar dotnet --version 返回 1.0.0-preview2-003121
  • 请尝试更新到最新版本(如果 Oleg 的回答尚未解决您的问题)。

标签: asp.net-core kestrel-http-server


【解决方案1】:

正确的语法是

dotnet run --server.urls=http://0.0.0.0:5001

而不是

dotnet run --server.urls http://0.0.0.0:5001

更多详情请见the old answer

【讨论】:

  • 仍在监听默认端口
  • @Set 你确定你通过了--server.urls=http://0.0.0.0:5002?在您更新的问题中,您使用--server.url(没有s)。
  • @TetraDev:抱歉,您应该准确地描述您的工作。例如,必须在Main 中调用.AddCommandLine(args) 并在project.json 中添加"Microsoft.Extensions.Configuration.CommandLine": "1.0.0" 才能使用AddCommandLine。您应该将您使用的Main 的代码与问题中发布的代码进行比较。
  • 正确的配置设置现在只是“urls”。因此,将您的 hosting.json 更改为 { "urls": "0.0.0.0:5001" }
  • @cbp:问题是关于“从命令行更改 Kestrel 侦听端口”。 hosting.json 的信息在 the old answer 中有描述,我也引用了。
猜你喜欢
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 2020-09-30
相关资源
最近更新 更多