问题描述
我希望Kestrel自动选择一个端口。我的Program.cs看起来像这样:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
var builder = webBuilder.UseStartup<Startup>();
if (args.Contains("--automatic-port-selection"))
{
builder.UseUrls("http://127.0.0.1:0");
}
});
我看着https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1来了解如何检测选定的端口。但是我实际上想在软件启动时获取端口。
我尝试了以下操作:
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more @R_33_4045@ion on how to configure your application,visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) { }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
var serverAddressesFeature =
app.ServerFeatures.Get<IServerAddressesFeature>();
if (serverAddressesFeature != null)
{
foreach (var address in serverAddressesFeature.Addresses)
{
int port = int.Parse(address.Split(':').Last());
Console.Out.WriteLine("Port:" + port);
}
Console.Out.Flush();
}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapGet("/",async context => { await context.Response.WriteAsync("Hello World!"); }); });
}
}
您基本上可以看到这只是从Visual Studio模板创建的aspnet核心应用程序。
这里的问题是,它总是总是只写0。似乎当我读serverAddressesFeature
时,它只能给出正确的端口号。处理请求时处理。启动服务器时如何获得使用的端口号?
编辑
这似乎是相关的:https://github.com/aspnet/Hosting/issues/1390
解决方法
据我所知,始终获得0端口号的原因是应用程序未完全启动。
应用程序启动时,将首先调用startup.cs配置方法。这次Kestrel知道其端口为0。
之后,它将找到自动释放端口。
因此,如果您想获取应用程序正在使用的端口。您可以编写一个方法,在应用程序完全声明要记录正确的端口后将触发该方法。
更多详细信息,您可以参考以下代码:
public void Configure(IApplicationBuilder app,IWebHostEnvironment env,IHostApplicationLifetime lifetime,IServiceProvider serviceProvider){
//other codes ....
lifetime.ApplicationStarted.Register(
() => LogAddresses(app.ServerFeatures));
}
static void LogAddresses(IFeatureCollection features)
{
var addressFeature = features.Get<IServerAddressesFeature>();
if (addressFeature != null)
{
foreach (var address in addressFeature.Addresses)
{
int port = int.Parse(address.Split(':').Last());
Console.Out.WriteLine("Port:" + port);
}
}
}
结果: