我有一个使用控制台与 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/