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
You can use the Start() method instead of Run() to access IServerAddressesFeature at the right moment:
IWebHost webHost = new WebHostBuilder()
.UseKestrel(options =>
options.Listen(IPAddress.Loopback, 0)) // dynamic port
.Build();
webHost.Start();
string address = webHost.ServerFeatures
.Get()
.Addresses
.First();
int port = int.Parse(address.Split(':').Last());
webHost.WaitForShutdown();