查找2个kestrel服务器的2个免费端口 [英] Find 2 different FREE ports for 2 kestrel servers

查看:53
本文介绍了查找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个免费端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆