.NET 2.0 WebException "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server"

I've been working with a few customers lately that have been experiencing this WebException that has been confirmed as an issue introduced by connection management design changes incorporated into .NET 2.0. The exception typically ocurrs during a web service call however any scenario involving a HttpWebRequest with keep-alives could encounter this issue. 

 

Note, this issue doesn't occur in .NET 1.x so if you are experiencing this exception in any .NET version prior to .NET 2.0 RTM then I suggest that you are probably dealing with a different issue so KB 915599 is probably a good place to start.

 

The issue involves how we handle the scenario whereby a server has sent a FIN to a keep-alive connection. We should close the connection and issue the next client request on a new connection. However, in .NET 2.0 RTM, we attempt to reuse the original connection resulting in the server resetting the connection on the next request. This reset results in this exception being raised in the client.

 

The issue is currently scheduled to be resolved in .NET 3.5. However, we are also considering the possibility of a hotfix for .NET 2.0.

 

If you suspect that you're hitting this issue, you’ll need to capture Netmon trace(s), ideally from both ends of the network conversation, whilst reproducing the issue. Generally speaking, it’s preferable to capture network traces from both ends of a conversation as this will allow you to determine if there is any possibility that intermediaries (ie proxy servers, networking hardware) are influencing the conversation.

 

If you’d like to minimise the size of the trace file, you can use a capture filter.

 

Resolutions D, E & F from KB 915599 are potential workarounds for you to consider. However, resolution D (ie disabling keep-alives) won’t be an option for you if you are using NTLM authentication as it requires keep-alives.

 

*** UPDATE *** The hotfix for .NET 2.0 is available via contacting Microsoft Customer Support Services and quoting KB 941633. https://support.microsoft.com/kb/941633