On this page

Web services - The underlying connection was closed

Ads

Navigation

Search

Categories

Clouds

Sql Server (5) .Net (16) .Net 2.0 (2) C# (3) @ff Topic (5) Architectural solutions (9) ASP (1) BDD (5) Blog related (8) database (2) Development process (8) Facebook (1) job interviews (1) Lessons (5) Life (12) Microsoft (5) IIS 6 (2) SPS (sharepoint server) (3) Drivers (1) Internet Explorer (2) Windows 2003 server (1) NightDuck (2) Performance (5) Security (9) Sql Server 2000 (4) Study (2) TDD (1) Threading (3) Under the hood (1) Web (1) Web services (1) XSS (6)

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 63
This Year: 7
This Month: 0
This Week: 0
Comments: 33

Sign In
Pick a theme:

 Sunday, September 30, 2007
Sunday, September 30, 2007 2:30:05 PM (GMT Standard Time, UTC+00:00) ( .Net 2.0 | C# | Web services )

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"

hmm, not a very nice exception.
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 :

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
{
/// <summary>
/// this method overides the base getWebRequest,
/// thus preventing the KeepAlive feature
/// </summary>
/// <param name="uri">the given uri</param>
/// <returns>the needed web request with the disabled keep alive feature</returns>
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)

/// <summary>
/// This method should disable the keep alive feature
/// </summary>
/// <param name="w">the needed HttpWebRequest</param>
/// <returns>Disabled HttpWebRequest</returns>
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

Comments [0] | | #