Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 防止红隼在使用端口时坠毁_Asp.net Core_.net Core_Kestrel Http Server - Fatal编程技术网

Asp.net core 防止红隼在使用端口时坠毁

Asp.net core 防止红隼在使用端口时坠毁,asp.net-core,.net-core,kestrel-http-server,Asp.net Core,.net Core,Kestrel Http Server,我怀疑这是一个'奇怪'的事情,我正试图这样做,但…我已配置红隼监听几个端口。问题是,有时,其中一个端口已经在使用中 这正在导致我的应用程序崩溃 我希望看到的行为是,它侦听我指定的所有可用端口。但是我还没有找到关于这个主题的任何示例/文档 例如,我可能会将其配置为在90、91、92、93上侦听…但如果端口91已在使用,我希望它只在端口90、92、93上侦听。我不介意它抛出异常或记录错误,只要我能让它继续。我希望避免“先检查”然后再更改配置,因为这感觉像是等待发生的竞争条件) 有人能告诉我如何允许K

我怀疑这是一个'奇怪'的事情,我正试图这样做,但…我已配置红隼监听几个端口。问题是,有时,其中一个端口已经在使用中

这正在导致我的应用程序崩溃

我希望看到的行为是,它侦听我指定的所有可用端口。但是我还没有找到关于这个主题的任何示例/文档

例如,我可能会将其配置为在90、91、92、93上侦听…但如果端口91已在使用,我希望它只在端口90、92、93上侦听。我不介意它抛出异常或记录错误,只要我能让它继续。我希望避免“先检查”然后再更改配置,因为这感觉像是等待发生的竞争条件)


有人能告诉我如何允许Kestrel仅在可用端口上启动吗?

您可以使用端口0;这样,Kestrel就可以在运行时动态绑定到一个可用的端口,如下所述:

私有静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.useURL(“http://*:0”);
此外,您还可以确定Kestrel在运行时实际绑定的端口,如下所示:

public static void Main(string[] args)
{
    IWebHost webHost = CreateWebHostBuilder(args).Build();

    webHost.Start();

    string address = webHost.ServerFeatures
        .Get<IServerAddressesFeature>()
        .Addresses
        .First();

    int port = int.Parse(address.Split(':')[4]);
}
publicstaticvoidmain(字符串[]args)
{
IWebHost webHost=CreateWebHostBuilder(args.Build();
webHost.Start();
字符串地址=webHost.ServerFeatures
.Get()
.地址
.First();
int port=int.Parse(address.Split(“:”)[4]);
}
更新:

如果指定的端口未被其他应用程序使用,则可以检查端口可用性并启动项目:

private static string[] GenerateUrls(IEnumerable<int> ports)
{
    return ports.Select(port => $"http://localhost:{port}").ToArray();
}

private static IEnumerable<int> GetAvailablePorts(IEnumerable<int> ports)
{
    IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
    IPEndPoint[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners();

    IEnumerable<int> allActivePorts = tcpConnInfoArray.Select(endpoint => endpoint.Port).ToList();

    return ports.Where(port => !allActivePorts.Contains(port)).ToList();
}
专用静态字符串[]生成器(IEnumerable端口)
{
返回端口。选择(端口=>$”http://localhost:{port}”).ToArray();
}
专用静态IEnumerable GetAvailableReports(IEnumerable端口)
{
IPGlobalProperties IPGlobalProperties=IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[]tcpconninforary=ipGlobalProperties.GetActiveTcpListeners();
IEnumerable allActivePorts=tcpConnInfoArray.Select(endpoint=>endpoint.Port.ToList();
返回端口。其中(端口=>!allActivePorts.Contains(端口)).ToList();
}
最终结果如下:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

private static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    IWebHostBuilder webHost = WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>();

    int[] ports = { 5000, 5050, 8585 };

    IEnumerable<int> availablePorts = GetAvailablePorts(ports).ToList();
    if (!availablePorts.Any())
        throw new InvalidOperationException("There are no active ports available to start the project.");

    webHost.UseUrls(GenerateUrls(availablePorts));

    return webHost;
}
publicstaticvoidmain(字符串[]args)
{
CreateWebHostBuilder(args.Build().Run();
}
私有静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)
{
IWebHostBuilder webHost=webHost.CreateDefaultBuilder(args)
.UseStartup();
int[]端口={500050508585};
IEnumerable AvailableReports=GetAvailableReports(ports).ToList();
如果(!availableport.Any())
抛出新的InvalidOperationException(“没有可用于启动项目的活动端口”);
UseUrls(GenerateUrls(availableport));
返回webHost;
}

我很欣赏这个答案,使用端口0听上去在很多情况下都很有用;但我需要它在多个特定端口上启动。如果这有意义的话。我试图编辑这个问题,使它更清楚。我想给它一个端口{20,21,22}的列表,并让它监听这三个端口中的任何一个。
public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

private static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    IWebHostBuilder webHost = WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>();

    int[] ports = { 5000, 5050, 8585 };

    IEnumerable<int> availablePorts = GetAvailablePorts(ports).ToList();
    if (!availablePorts.Any())
        throw new InvalidOperationException("There are no active ports available to start the project.");

    webHost.UseUrls(GenerateUrls(availablePorts));

    return webHost;
}