查找2个kestrel服务器的2个免费端口

编程入门 行业动态 更新时间:2024-10-25 16:25:41
本文介绍了查找2个kestrel服务器的2个免费端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要从控制台应用程序启动2个Kestrel服务器.下面的代码显示了我现在的操作方式.

I need to start 2 Kestrel servers from a Console application. The code below shows how I'm doing it now.

不幸的是,两个服务器都尝试在相同的端口 HTTP:5000 和 HTTPS:5001 上启动,而实际上只有第一个启动.

Unfortunately, both servers attempt to start on the same ports HTTP:5000 and HTTPS:5001 and only first one is actually started.

我还尝试在 appsettings.json 中指定 URL ,但是它不起作用符合预期,并且我不希望对服务器URL进行硬编码,因为如果我重新启动控制台应用程序,它不会杀死先前启动的服务器,也无法再次启动它们.

I also tried to specify URLs in appsettings.json but it doesn't work as expected and I wouldn't like to hardcode server URLs, because if I restart Console app it doesn't kill previously started servers and can't start them again.

问题

如何从代码为两个服务器为HTTP和HTTPS查找可用端口,并确保它们不同?

How to find free ports for HTTP and HTTPS for both servers from code and make sure that they are different?

服务器

public class WebServer { public static IWebHost Run<TStartup>(WebOptions options = null) { var configuration = new ConfigurationBuilder().Build(); var environment = WebHost .CreateDefaultBuilder(new string[0]) .ConfigureServices(o => o.AddSingleton(options)) .UseConfiguration(configuration) .UseContentRoot(Directory.GetCurrentDirectory()) .UseKestrel() .UseStartup<TStartup>() .Build(); environment.RunAsync(); return environment; } } var serviceEnvironment = Server.Run<ServiceStartup>(); var webEnvironment = Server.Run<WebStartup>(); var serviceAddresses = serviceEnvironment.ServerFeatures.Get<IServerAddressesFeature>().Addresses; var webAddresses = webEnvironment.ServerFeatures.Get<IServerAddressesFeature>().Addresses;

推荐答案

您可以绑定到端口0,Kestrel会自动找到一个随机的可用端口.

You can bind to port 0 and Kestrel will find a random available port automatically.

来自针对Kestrel的Microsoft文档:

当指定端口号0时,Kestrel动态绑定到可用端口.以下示例显示了如何确定哪个Kestrel端口实际上是在运行时绑定的:

When the port number 0 is specified, Kestrel dynamically binds to an available port. The following example shows how to determine which port Kestrel actually bound at runtime: public void Configure(IApplicationBuilder app) { var serverAddressesFeature = app.ServerFeatures.Get<IServerAddressesFeature>(); app.UseStaticFiles(); app.Run(async (context) => { context.Response.ContentType = "text/html"; await context.Response .WriteAsync("<!DOCTYPE html><html lang=\"en\"><head>" + "<title></title></head><body><p>Hosted by Kestrel</p>"); if (serverAddressesFeature != null) { await context.Response .WriteAsync("<p>Listening on the following addresses: " + string.Join(", ", serverAddressesFeature.Addresses) + "</p>"); } await context.Response.WriteAsync("<p>Request URL: " + $"{context.Request.GetDisplayUrl()}<p>"); }); }

更多推荐

查找2个kestrel服务器的2个免费端口

本文发布于:2023-08-07 13:43:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   服务器   kestrel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!