February 2008
Posted on the 16th at 12:51 AM CST
The Underlying Connection was Closed - How to Fix This Pesky ASP.NET WebException
FiledFiled under ASP.NET, VB.NET

If your ASP.NET application calls a web service (you have a reference in your App_WebReferences folder), you might come across this fairly typical System.Net.WebException. I have seen two different breeds of this exception, and the solutions for each of them are quite similiar. I will start off with the most common of the two.

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

This is the one you are most likely to see, and the fix for this is simple and generally well-known. You need to create a class that inherits your web service class. Then you simply override the GetWebRequest function and modify the KeepAlive HTTP header accordingly…

Public Class MyWebService
    Inherits ServiceName.WebService

    Protected Overrides Function GetWebRequest(ByVal uri As Uri) As System.Net.WebRequest
        Dim webRequest As System.Net.HttpWebRequest = MyBase.GetWebRequest(uri)

        webRequest.KeepAlive = False

        Return webRequest
    End Function
End Class


Simple as that. When you need to invoke the web service elsewhere in your code, just instatiate that class instead of what you would do normally…

Dim Response as String

Using MyService as New MyWebService()
    Response = MyService.METHOD_NAME("MethodInput")
End Using


And that takes care of that exception.

The underlying connection was closed: The connection was closed unexpectedly.

Sometimes, using the solution above will cause this exception. I believe that it has something to do with the way Windows Authentication is configured in IIS. I ran across a situation where I had to deploy my applicaton (with web service references) to a remote server that was not in our control; it was being hosted for us. Apparently, they were using NTLM Authentication, which requires connections to be kept-alive, hence this exception. However, using KeepAlive makes your requests totally inconsistent, in most cases. They will work one time, but will fail the next. The host was not willing to change for this for our application, obviously, so I was stuck. Finally, I was able to determine that the ConnectionGroupName property is what is used to re-establish the connection that was supposed to be "kept alive". So, you can just set that property to a random value (GUID), in the same way that I set the KeepAlive property above…

Public Class MyWebService
    Inherits ServiceName.WebService

    Protected Overrides Function GetWebRequest(ByVal uri As Uri) As System.Net.WebRequest
        Dim webRequest As System.Net.HttpWebRequest = MyBase.GetWebRequest(uri)

        webRequest.ConnectionGroupName = Guid.NewGuid().ToString()

        Return webRequest
    End Function
End Class


Problem solved; that worked fantastic! I hope this has helped you out, and I also hope that I have had my final bout with the System.Net.WebException.

Comments (2)
Permalink Comment from An old hacker on May 16th, 2008 at 10:19 AM
Recent studies from independant companies proved that even if IE has more security problem, those problems are minor, Firefox security problems are less in number but by far a worst threat (hackers could take control of the user system)

But I guess that you're not able to program correctly for multiple browsers.
Permalink Comment from Josh StodolaEmail on May 16th, 2008 at 11:58 AM
Pardon? I don't know what the hell you are talking about! I am able to "program correctly" for all modern browsers. Regardless, that has nothing to do with your first statement. Keep your comments constructive, please.

Guess What?

There are a few basic guidelines you should be aware of before leaving a comment…

  • If you choose to display your email address, it will not be detected by spam bots
  • Comments are limited to 3,000 characters; so far you have used none of them
  • HTML will be encoded; links and line breaks will be converted automatically
  • Comments containing five or more links will be subject to moderation

Have Your Say

← Answer this to prove you are human
 
 

Chill Out…

No Trackbacks