Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reproduce "A connection that was expected to be kept alive was closed by the server."

Tags:

c#

We're using WebClient, .NET 3.5sp1 in a winforms application. For some users this results in an Exception with the message:

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

Searching a bit around the web suggests a "fix" to just disable http keepalive, which we're not really interested in doing, some suggests it might be a bug in the .NET libraries, etc.

The error message suggests it's a keepaliv'ed http connection that somehow got closed by the server(or a proxy) without the underlyings of WebClient detecting it properly.

We're thinking of catching this specific case, and simply try the request again. However we cannot reproduce this exception. So.

  1. How can we properly catch the case that yields the above error message.

    catch(WebException ex) { if (ex.Message == "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server") { ... }

    smells.. bad.

  2. Any tips on how we can reproduce the above exception ?

like image 467
Habalusa Avatar asked Jan 06 '11 10:01

Habalusa


2 Answers

WebClient detects this just fine. Thus the exception. You need to find the server that's misbehaving. Not quite sure what to do if you find that server, maybe you can send the admin a nice email message.

Log the server URL.

like image 161
Hans Passant Avatar answered Sep 25 '22 15:09

Hans Passant


I suggest you have a look at this blog by Misrosoft: Http Client Protocol Issues

like image 26
Robert Avatar answered Sep 24 '22 15:09

Robert