Exe调用WebAPI时5分钟连接被强制关闭,超时设置无效求助
Hey there, let's tackle this super frustrating 5-minute connection drop issue you're dealing with—it makes no sense when the timeout you set in code gets ignored, right? The core problem here is that your client-side timeout (3600 seconds) isn't the one calling the shots; the disconnect is coming from somewhere else. Let's break down the fixes step by step:
1. Check WebAPI Server Timeout Configurations
Most web servers have default 5-minute limits that override your client settings. Start here:
If your WebAPI uses Kestrel:
Update the KeepAliveTimeout and RequestTimeout in your Program.cs to override the default 5-minute cap:
builder.WebHost.ConfigureKestrel(options => { // Set request timeout to 1 hour options.Limits.RequestTimeout = TimeSpan.FromHours(1); // Extend keep-alive timeout to 1 hour options.Limits.KeepAliveTimeout = TimeSpan.FromHours(1); });
If your WebAPI is hosted on IIS:
- Open IIS Manager, navigate to your site → Advanced Settings:
- Adjust the Connection Timeout from the default 120 seconds to 3600 seconds;
- Go to your app pool → Advanced Settings:
- Increase the Idle Time-out (default 20 minutes) to a value longer than your expected request duration;
- If using ARR (Application Request Routing):
- Access ARR's Server Proxy Settings and set the timeout to 3600 seconds.
2. Enable TCP Keep-Alive on the Client
Even if you fix the server, firewalls or gateways often kill idle connections after 5 minutes. Keep the connection active with these settings:
For HttpClient:
Use a custom SocketsHttpHandler to send periodic keep-alive pings:
var handler = new SocketsHttpHandler { KeepAlivePingDelay = TimeSpan.FromMinutes(1), // Send a keep-alive ping every minute KeepAlivePingTimeout = TimeSpan.FromSeconds(30), PooledConnectionIdleTimeout = TimeSpan.FromHours(1), PooledConnectionLifetime = TimeSpan.FromHours(1) }; using var httpClient = new HttpClient(handler) { Timeout = TimeSpan.FromHours(1) };
For HttpWebRequest:
Adjust ServicePointManager settings to enable TCP keep-alive:
// Turn on TCP keep-alive: start sending pings after 1 minute, repeat every 10 seconds ServicePointManager.SetTcpKeepAlive(true, 60000, 10000); ServicePointManager.MaxServicePointIdleTime = 3600000; // 1-hour idle timeout var request = (HttpWebRequest)WebRequest.Create("your-api-endpoint"); request.Timeout = 3600000; // 1-hour request timeout request.ReadWriteTimeout = 3600000;
3. Check Network Device Timeouts
If the above steps don't work, the issue is almost certainly with a firewall, VPN, or load balancer in your network. Reach out to your network admin to:
- Confirm if these devices have a default 300-second (5-minute) idle connection timeout;
- Request to extend the timeout for traffic between your .exe client and WebAPI server to at least 3600 seconds;
- Ensure the devices allow TCP keep-alive packets to pass through, so your client's pings can maintain the connection.
4. Verify the API Request is Actually Running
Last but not least, make sure your WebAPI isn't crashing or ending the request early. Add detailed logging in your API to track when the request starts, key processing milestones, and when it finishes. This will rule out server-side errors that might be closing the connection before your client timeout kicks in.
内容的提问来源于stack exchange,提问作者Eraiarasu

