With ASP.NET Core 3.0 I have been able to use this IHostedService
method ...
Determine port Kestrel binded to
... to determine Kestrel's dynamic port at run-time.
IServerAddressesFeature
is documented for ASP.NET 3.0 here:
But when changing the version to ASP.NET Core 3.1 then the page redirects back to ASP.NET 3.0 with the hint that the document is not available for ASP.NET Core 3.1. Is IServerAddressesFeature
no longer working? Using IServerAddressesFeature
with ASP.NET Core 3.1 still compiles, but the port in the returned ServerAddresses
is always zero.
Program:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost().Run();
}
public static IWebHost BuildWebHost() =>
WebHost.CreateDefaultBuilder()
.UseKestrel()
.UseUrls("http://127.0.0.1:0") // port zero to use random dynamic port
.UseStartup<Startup>()
.Build();
}
Later, when Configure
is called ...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
HostedService.ServerAddresses = app.ServerFeatures.Get<Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature>();
... the server addresses are assigned to the HostedService.ServerAddresses
static variable as in the linked example. However, ServerAddresses
contain only the loopback address with port zero: "http://127.0.0.1:0"
.
Am I overlooking something? Is there a different, proper way to resolve the problem in v3.1? How do I configure Kestrel to use a random dynamic port and determine which port it is at run-time (before any controller action takes place) with ASP.NET Core 3.1?
Update
Here is an ugly workaround that helps determine the port. It appears that the timing or sequence of dynamic port allocation has changed. Returning from the HostedService.StartAsync
method and reading the server address a bit later seems to be enough. Surely there must be a better way?
public Task StartAsync(CancellationToken cancellationToken)
{
System.Threading.Tasks.Task.Run(async () =>
{
int port = 0;
while (port == 0)
{
await System.Threading.Tasks.Task.Delay(250);
var address = ServerAddresses.Addresses.FirstOrDefault();
if (string.IsNullOrEmpty(address))
continue;
// address is always in form http://127.0.0.1:port
var pos = address.LastIndexOf(':');
if (pos > 0)
{
var portString = address.Substring(pos + 1);
port = int.Parse(portString);
}
}
// have determined the dynamic port now
});
return System.Threading.Tasks.Task.CompletedTask;
}