Configurable port number when using Kestrel?

2019-05-29 04:17发布

I have done the following but it still doesn't work. Running dotnet myapp.dll still shows it's listening http://localhost:5000.

  1. Create hosting.json

Code:

{
  "server.url": "http://*:5001"
}
  1. Updated Program.cs

Code:

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

        var host = new WebHostBuilder()
            .UseConfiguration(config) // added
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            //.UseUrls("http://*:5001")
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}
  1. Updated project.json

Code:

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config",
      "NLog.config",
      "hosting.json"
    ]

1条回答
时光不老,我们不散
2楼-- · 2019-05-29 05:00
  1. You need to change order: .SetBasePath should be called before file reading

    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
        .Build();
    
  2. Use server.urls, not server.url

查看更多
登录 后发表回答