Search code examples
c#.nettimeouthttpwebrequest

Webrequest timed out even with infinite timeout set


I have a C# method that open a HttpWebRequest with an external node module.

Then execute some long DB queries, process those data and send them to the node module.

The node module receive throught a socket, data from c#, and it will turn this data to other data that will be turned back to c#.

For some tests that take ~30min of process, everything went okay. But a bigger test that took around 2hr, I've got the request was aborted the operation has timed out

Here is a part of my code :

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
request.Timeout = -1;
request.KeepAlive = false; // both true & false values were tested, but gave the same result
request.ServicePoint.ConnectionLeaseTimeout = -1;
request.ServicePoint.MaxIdleTime = -1;
Stream st = request.GetRequestStream();
StreamWriter stw = new StreamWriter(st);
stw.Write("");

//Long process of data, queries executions and writing in stw that is received by the node module

//this line is where it throw the exception of timeout, and here is where I'm supposed to get the output from the node module, based on what I've sent
Stream stmResponse = request.GetResponse().GetResponseStream();

The connection shouldn't be dropped off, as it's a continuous sending of data from C# that is processed by the node module instantly.

I've also change the timeout config in web.config

  <system.web>
    <httpRuntime executionTimeout="180000" /> ...

Solution

  • I figure out the source of the issue, which isn't related to the code at all, as the connection with the external module where done over a VPN and a Firewall is in place, and both were cutting the connection in a certain time.

    After removing both VPN & Firewall, the timeout exception weren't thrown anymore.