Web services - "The underlying connection was closed.." error

Recently i encountered some errors with web services regarding webService response.

The full Exception that was thown was :
"Server was unable to process request. -> the underlying connection was closed: A connection that was expected to be kept alive was closed by the server"

Digging a little in , i found few reasons that may cause that error.

idle time on the client side is greater than on the server side.
network issues that prevent the Keep-Alive feature from being committed. (firewalls, proxies and so...).

what is this KeepAlive feature : this "great" feature actually keeps the connection between the WebService client and server open for a defined amount of time,thus enhancing WebService interaction performance by eliminating the need to open a new connection each time.

unfortunately, the time for that window is not synchronized between the client and the server. this situation creates a problem, on which the client may think that the connection is still open, but the server closed it.

so, what can be done so solve that matter ?

ensure the reasons above would not happen disable the KeepAlive feature.
since, ensuring that our time is not greater than the one on the server side is impossible when dealing with 3rd party services, not even mentioning the 3rd party network configuration, the optimal solution for this issue will be disabling the Keep-Alive feature.

how can we do that ?

when adding a web reference with visual studio 2005, it automatically generates a file called reference.cs which is used as a proxy to the web service on the server.
this is the place which we should do the "fix".

there are 2 ways to prevent this certain problem from happening :

set the KeepAlive property to false
use the HTTP protocol version 1.0 (difference between http 1.0 and 1.1)

since our "proxy" is being generated each time we update our web reference, we would not want the change to be made in the reference.cs file, since it can be overridden the next time we will do "update web reference"

what we can do, is use the "partial class" feature that presented in .Net 2.0 framework to solve this issue.

namespace theNamespaceOfYourServiceAsStatedInTheReferenceFile
{
public partial class MyService
{
///




/// this method overides the base getWebRequest,
/// thus preventing the KeepAlive feature
///

/// the given uri
/// the needed web request with the disabled keep alive feature
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
// Do the base class operation and obtain the specific web request
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

// do the keep alive handling
return Utils.DisableKeepAlive(webRequest);
}
}


now we need to decide how we are going the disable it(write the DisableKeepAlive in our utils)

///



/// This method should disable the keep alive feature
///

/// the needed HttpWebRequest
/// Disabled HttpWebRequest
public static HttpWebRequest DisableKeepAlive(HttpWebRequest w)
{
// Set the keep-Alive to false, thus for not using it
w.KeepAlive = false;


// the second way
w.ProtocolVersion = System.Net.HttpVersion.Version10;

return w;
}


Thats it, now we have successfully disabled the KeepAlive feature

Read Users' Comments (0)

0 Response to "Web services - "The underlying connection was closed.." error"

Post a Comment