c# - CentOS 服务器上的 ASP .NET 核心控制台应用程序丢弃请求

标签 c# asp.net linux centos .net-core

我有一个使用控制台与 ASP .Net 核心一起运行的应用程序(不是 Web 应用程序,没有 IIS)。在 Windows 上,一切一直都运行良好。但是当我在我们的服务器 (CentOS 7) 上运行它时,大多数请求都被丢弃了。

我的开始方法是:

        ...
        var config = new ConfigurationBuilder()
            .AddCommandLine(new string[] { $"--server.urls={urls}" })
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseStartup<Listener>()
            .UseLoggerFactory(loggerFactory)
            .Build();

        host.Start();

        ConsoleKeyInfo key;
        while ((key.Modifiers & ConsoleModifiers.Control) != ConsoleModifiers.Control && key.Key != ConsoleKey.Q)
        {
            key = Console.ReadKey();
        }

        host.Dispose();

和听众:

public class Listener
{
    ...

    public void ConfigureServices(IServiceCollection collections)
    {
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.Run(async (context) =>
        {
            ...

            context.Response.StatusCode = *** some response code ***

            using (StreamWriter writer = new StreamWriter(context.Response.Body))
                await writer.WriteLineAsync(*** a response ***);
        });
    }
}

如果用 curl 尝试一些请求

curl -v http://x.x.x.x:1234/

然后大部分时间我收到:

* timeout on name lookup is not supported
*   Trying x.x.x.x...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* connect to x.x.x.x port 1234 failed: Connection refused
* Failed to connect to x.x.x.x port 1234: Connection refused
* Closing connection 0
curl: (7) Failed to connect to x.x.x.x port 1234: Connection refused

但只是在大多数时候,这意味着相同的命令有时会起作用。

而且,如果我使用 Postman,那么 10 次中有 9 次它可以正常工作。

我到处都看了,我真的迷路了,如果有人有想法请告诉我。

最佳答案

我发现了问题。与 ASP .net 或 CentOS 无关。这是我们的防火墙(在 vpn 后面),路由规则由端口 ex 的范围设置:从 ip1 端口 10000-10010 -> 到 ip2 端口 10000-10010。因此,如果在 ip1 端口 10001 上收到一条消息,我假设它会被发送到 ip2 端口 10001。但不,它被发送到范围内的随机端口。

关于c# - CentOS 服务器上的 ASP .NET 核心控制台应用程序丢弃请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42070135/

相关文章:

asp.net - 如何获取动态创建的文本框的文本值

fnctl(F_SETOWN, <pid>) 能否将信号定向到 pthread ID 而不是进程 ID?

java - 可以有没有设置时区的机器吗?

c# - 为什么将我们的 .NET/SQL Server 网站移动到新主机会导致超过连接池大小?

c# - 在 web api 和 odata 中处理大型数据集

c# - 我可以在没有定时器组件的情况下使用定时器吗?

javascript - 如何在 View MVC 中更改 img src

python - 设置环境变量,使其可以在我的程序之外读取

c# - DateTime 可选参数的默认值应该是多少?

c# - 如何在 xamarin.forms 中隐藏 ListView 的空行