Mike Mooney
Mike Mooney

Reputation: 11989

Oubound HttpWebRequest in ASP.NET still timing out after destination server comes back up

We have a ASP.NET web application (MVC3) that makes calls out to another web server using HttpWebRequest.

Anyhow, when the remote server goes down, we start getting timeout errors, which we would expect.

However, then when the remote server comes back up, we continue to get timeout errors. Refreshing the application pool solves the issue, but we really don't want to have to restart the app pool every time.

Is there pooling that is going on that could be storing the bad timed-out connections? My expectation would be that if the connection throws an error, it would get disposed, and then we'd get a new connection the next time, but that doesn't seem to be the case.

Here's what our code looks like:

var request = (HttpWebRequest)HttpWebRequest.Create(url);
int timeout = GetTimeout();
request.ReadWriteTimeout = timeout;

WebRequest.DefaultWebProxy = null; 

var asyncResult = request.BeginGetResponse(null, null);
bool complete = asyncResult.AsyncWaitHandle.WaitOne(timeout);
if (!complete)
{
    ThrowTimeoutError(url, timeout);
}
using (var webResponse = request.EndGetResponse(asyncResult))
{
    using (var responseStream = webResponse.GetResponseStream())
    {
        using (var responseStreamReader = new StreamReader(responseStream))
        {
            responseXml = responseStreamReader.ReadToEnd();
        }
    }
}

Upvotes: 1

Views: 135

Answers (1)

Paul Sasik
Paul Sasik

Reputation: 81509

Have you seen/tried the CloseConnectionGroup method of the ServicePoint class?

From the MSDN documentation:

Connection groups associate a set of requests with a particular connection or set of connections. This method removes and closes all connections that belong to the specified connection group.

Calling this method may reset your connections without having to restart the pool.

Upvotes: 1

Related Questions