title: WebRequest and Resources - ASP.NET Timeout
body: <p>I have a ASP.NET Web Forms application that internally makes many SOAP and REST calls to web services. The SOAP calls are made using Microsoft's own "wrapping" code. The REST calls are made from a simple REST client. This client uses Using blocks to dispose of any resources.</p>
  
  <p>The application runs fine for a few hours but then hangs up. The site will no longer be able to serve up asp.net pages. I tested the site by putting a hello.htm file on it... it served up fine. So it is definitely a problem in the ASP.NET engine space.</p>
  
  <p>It feels like it has run out of webrequests from a pool. I could be way off. What can I try? What should I look at? It takes hours to get the problem to repro.
  Restarting the site on IIS fixes the issue, but of cource is not really a fix we can live with.</p>
  
  <p><strong>start of error and stack trace:</strong><br>
  Server Error in '/' Application.</p>
  
  <p>The operation has timed out 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <p>Exception Details: System.Net.WebException: The operation has timed out</p>
  
  <p>Source Error: 
  An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. </p>
  
  <p>Stack Trace: 
  [WebException: The operation has timed out]
  System.Net.HttpWebRequest.GetRequestStream() +5322142
  System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +103</p>
  
  <p><strong>UPDATE and MORE INFO</strong><br>
  The homegrown REST calls and SOAP are all done synchronous.<br>
  They are not long-lived they take about 1 sec.<br>
  Web Services are hosted on a Tomcat server (different machine) in the same data center.<br>
  The .NET app calls SOAP and REST services on the Tomcat server.</p>
  
  <p>The app somehow fixed itself after about an hour of staying in this hung state. Thoughts?
  How do I monitor the .NET Threadpool? Does DefaultConnectionLimit affect the self-initiated outbound connections? </p>
  
  <p><strong>RESOLUTION</strong>
  see additional comments to answers and...
  1) close all streams and responses/requests from your homegrown REST code (oops)<br>
  2) System.Net.ServicePointManager.DefaultConnectionLimit = 96;<br>
     // default = 12 times # of cores<br>
     System.Net.ServicePointManager.MaxServicePointIdleTime= 3000;<br>
     // default = 100000 (100 seconds)<br>
  By fixing any leaky code and timing out the IdleTime faster everything seems to work fine now</p>