Configurable port number when using Kestrel?

∥☆過路亽.° 提交于 2019-12-08 02:03:48

问题


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:


  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



来源:https://stackoverflow.com/questions/39581010/configurable-port-number-when-using-kestrel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!