How to determine which port ASP.NET Core 2 is listening on when a dynamic port (0) was specified

前端 未结 5 1164
遇见更好的自我
遇见更好的自我 2020-12-17 20:28

I\'ve got an ASP.NET Core 2.0 app which I intend to run as a stand-alone application. The app should start up and bind to an available port. To achieve this I configure the

5条回答
  •  旧时难觅i
    2020-12-17 21:23

    You can achive it in Startup class in method Configure. You can get port from ServerAddressesFeature

    Here is an example of code:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ILogger logger)
    {
         var serverAddressesFeature = app.ServerFeatures.Get();
    
         loggerFactory.AddFile("logs/myfile-{Date}.txt", minimumLevel: LogLevel.Information, isJson: false);
    
         logger.LogInformation("Listening on the following addresses: " + string.Join(", ", serverAddressesFeature.Addresses));
    }
    

提交回复
热议问题